std::rotate
提供: cppreference.com
![]() | このページは、Google 翻訳を使って英語版から機械翻訳されました。 翻訳には誤りや奇妙な言い回しがあるかもしれません。文章の上にポインタをおくと、元の文章が見れます。誤りを修正して翻訳を改善する手助けをしてください。翻訳についての説明は、ここをクリックしてください。 |
Defined in header <algorithm> | ||
template<class ForwardIt > void rotate( ForwardIt first, ForwardIt n_first, ForwardIt last ); | (C++11以前) (C++11およびそれ以降) | |
スワップは、要素
[first, last)
は新しい範囲の最初の要素となり、n_first
最後の要素となるように範囲n_first - 1
内の要素、. Original:
Swaps the elements in the range
[first, last)
in such a way, that the element n_first
becomes the first element of the new range and n_first - 1
becomes the last element. The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集]パラメータ
first, last | - | 回転させるための要素の範囲 Original: the range of elements to rotate The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
n_first | - | 新しい範囲の先頭に移動する要素 Original: the element to move to the beginning of the new range The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
型の要件 | ||
-ForwardIt は ValueSwappable and ForwardIterator 。 | ||
-The type of dereferenced ForwardIt must meet the requirements of MoveAssignable and MoveConstructible . |
[編集]値を返します
(なし)(C++11以前)
Original:
(none) (C++11以前)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
first + (last - n_first)
(C++11およびそれ以降)反復等しいOriginal:
The iterator equal to
first + (last - n_first)
(C++11およびそれ以降)The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]複雑性
first
last
との間の距離の線形Original:
linear in the distance between
first
and last
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集]可能な実装
template<class ForwardIt>void rotate(ForwardIt first, ForwardIt n_first, ForwardIt last){ ForwardIt next = n_first;while(first != next){std::swap(*first++, *next++);if(next == last){ next = n_first;}elseif(first == n_first){ n_first = next;}}} |
[編集]例
はstd :: rotateは、多くのアルゴリズムに共通のビルディング·ブロックです。この例では、挿入ソートを示し、C + +
Original:
std::rotate is a common building block in many algorithms. This example demonstrates insertion sort in C++
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
このコードを実行します
#include <vector>#include <iostream>#include <algorithm> int main(){std::vector<int> v{2, 4, 2, 0, 5, 10, 7, 3, 7, 1}; std::cout<<"before sort: ";for(int n: v)std::cout<< n <<' ';std::cout<<'\n'; // insertion sortfor(auto i = v.begin(); i != v.end();++i){ std::rotate(std::upper_bound(v.begin(), i, *i), i, i+1);} std::cout<<"after sort: ";for(int n: v)std::cout<< n <<' ';std::cout<<'\n'; // simple rotation to the left std::rotate(v.begin(), v.begin()+1, v.end()); std::cout<<"simple rotate left : ";for(int n: v)std::cout<< n <<' ';std::cout<<'\n'; // simple rotation to the right std::rotate(v.rbegin(), v.rbegin()+1, v.rend()); std::cout<<"simple rotate right : ";for(int n: v)std::cout<< n <<' ';std::cout<<'\n'; }
出力:
before sort: 2 4 2 0 5 10 7 3 7 1 after sort: 0 1 2 2 3 4 5 7 7 10 simple rotate left : 1 2 2 3 4 5 7 7 10 0 simple rotate right: 0 1 2 2 3 4 5 7 7 10
[編集]参照
コピーと要素の範囲を回転させます Original: copies and rotate a range of elements The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) |