共用方式為


rotate_copy

若要在兩個相鄰的範圍的項目在來源範圍內並複製結果到目的範圍。

template<class ForwardIterator, class OutputIterator>
   OutputIterator rotate_copy(
      ForwardIterator _First, 
      ForwardIterator _Middle,
      ForwardIterator _Last, 
      OutputIterator _Result
   );

參數

  • _First
    處理順向 Iterator 的第一個項目的位置在範圍會旋轉。

  • _Middle
    定義在處理第一個項目位置範圍中的第二個部分中項目將切換與該範圍內的第一部分之範圍內的順向 Iterator 界限。

  • _ Last
    處理順向的 Iterator 超過最後一個項目的位置是在範圍會旋轉。

  • _Result
    解決輸出 Iterator 的第一個項目的位置在目的範圍。

傳回值

解決輸出的 Iterator 超過最後一個項目的位置是在目的範圍。

備註

範圍的參考必須是有效的,任何指標必須 dereferenceable,並在序列中最後一個位置開始可取得的會增加。

複雜度是線性與最多_Last (–) _First交換。

rotate_copy 有兩個關聯的表單:

如需這些函式如何運作的詳細資訊,請參閱 檢查過的 Iterator

範例

// alg_rotate_copy.cpp
// compile with: /EHsc
#include <vector>
#include <deque>
#include <algorithm>
#include <iostream>

int main() {
   using namespace std;
   vector <int> v1 , v2 ( 9 );
   deque <int> d1 , d2 ( 6 );
   vector <int>::iterator v1Iter , v2Iter;
   deque<int>::iterator d1Iter , d2Iter;

   int i;
   for ( i = -3 ; i <= 5 ; i++ )
      v1.push_back( i );

   int ii;
   for ( ii =0 ; ii <= 5 ; ii++ )
      d1.push_back( ii );

   cout << "Vector v1 is ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ;v1Iter ++ )
      cout << *v1Iter  << " ";
   cout << ")." << endl;

   rotate_copy ( v1.begin ( ) , v1.begin ( ) + 3 , v1.end ( ) , v2.begin ( ) );
   cout << "After rotating, the vector v1 remains unchanged as:\n v1 = ( " ;
   for ( v1Iter = v1.begin( ) ; v1Iter != v1.end( ) ;v1Iter ++ )
      cout << *v1Iter  << " ";
   cout << ")." << endl;

   cout << "After rotating, the copy of vector v1 in v2 is:\n v2 = ( " ;
   for ( v2Iter = v2.begin( ) ; v2Iter != v2.end( ) ;v2Iter ++ )
      cout << *v2Iter  << " ";
   cout << ")." << endl;

   cout << "The original deque d1 is ( " ;
   for ( d1Iter = d1.begin( ) ; d1Iter != d1.end( ) ;d1Iter ++ )
      cout << *d1Iter  << " ";
   cout << ")." << endl;

   int iii = 1;
   while ( iii <= d1.end ( ) - d1.begin ( ) )
   {
      rotate_copy ( d1.begin ( ) , d1.begin ( ) + iii , d1.end ( ) , d2.begin ( ) );
      cout << "After the rotation of a single deque element to the back,\n d2 is   ( " ;
      for ( d2Iter = d2.begin( ) ; d2Iter != d2.end( ) ;d2Iter ++ )
         cout << *d2Iter  << " ";
      cout << ")." << endl;
      iii++;
   }
}

Output

Vector v1 is ( -3 -2 -1 0 1 2 3 4 5 ).
After rotating, the vector v1 remains unchanged as:
 v1 = ( -3 -2 -1 0 1 2 3 4 5 ).
After rotating, the copy of vector v1 in v2 is:
 v2 = ( 0 1 2 3 4 5 -3 -2 -1 ).
The original deque d1 is ( 0 1 2 3 4 5 ).
After the rotation of a single deque element to the back,
 d2 is   ( 1 2 3 4 5 0 ).
After the rotation of a single deque element to the back,
 d2 is   ( 2 3 4 5 0 1 ).
After the rotation of a single deque element to the back,
 d2 is   ( 3 4 5 0 1 2 ).
After the rotation of a single deque element to the back,
 d2 is   ( 4 5 0 1 2 3 ).
After the rotation of a single deque element to the back,
 d2 is   ( 5 0 1 2 3 4 ).
After the rotation of a single deque element to the back,
 d2 is   ( 0 1 2 3 4 5 ).

需求

標題: <algorithm>

命名空間: std

請參閱

參考

rotate_copy (STL Samples)

標準樣板程式庫