unique_copy

複製來源範圍中之項目的目的範圍但彼此相鄰的重複的項目。

template<class InputIterator, class OutputIterator>
   OutputIterator unique_copy(
      InputIterator _First, 
      InputIterator _Last, 
      OutputIterator _Result
   );
template<class InputIterator, class OutputIterator, class BinaryPredicate>
   OutputIterator unique_copy(
      InputIterator _First, 
      InputIterator _Last, 
      OutputIterator _Result,
      BinaryPredicate _Comp,
   );

參數

  • _First
    處理順向 Iterator 的第一個項目的位置在要複製的來源範圍。

  • _Last
    處理順向的 Iterator 超過最後一個項目的位置是在要複製的來源範圍。

  • _Result
    解決輸出 Iterator 的第一個項目的位置會受到都已移除的連續重複複本的目的範圍。

  • _Comp
    定義要滿足條件的使用者定義之述詞函式物件,如果兩個項目將會視為相等。一個二進位述詞採用兩個引數並傳回 true ,當內容和 false ,則內容。

傳回值

解決輸出的 Iterator 超過最後一個項目的位置是在接收都已移除的連續重複複本的目的範圍。

備註

演算法的兩個表單移除一個連續字組中的第二個複本的相等項目。

演算法的作業是穩定的,使中取消刪除的項目的相對順序並不會變更。

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

複雜的屬性,以便 (_Last – _First) 比較。

unique_copy 有兩個關聯的表單:

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

範例

// alg_unique_copy.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <ostream>

using namespace std;

// Return whether modulus of elem1 is equal to modulus of elem2
bool mod_equal ( int elem1, int elem2 ) {
   if ( elem1 < 0 ) 
      elem1 = - elem1;
   if ( elem2 < 0 ) 
      elem2 = - elem2;
   return elem1 == elem2;
};

int main() {
   vector <int> v1;
   vector <int>::iterator v1_Iter1, v1_Iter2,
         v1_NewEnd1, v1_NewEnd2;

   int i;
   for ( i = 0 ; i <= 1 ; i++ ) {
      v1.push_back( 5 );
      v1.push_back( -5 );
   }

   int ii;
   for ( ii = 0 ; ii <= 2 ; ii++ )
      v1.push_back( 4 );
   v1.push_back( 7 );

   int iii;
   for ( iii = 0 ; iii <= 5 ; iii++ )
      v1.push_back( 10 );
   
   cout << "Vector v1 is\n ( " ;
   for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1.end( ) ; v1_Iter1++ )
      cout << *v1_Iter1 << " ";
   cout << ")." << endl;

   // Copy first half to second, removing consecutive duplicates
   v1_NewEnd1 = unique_copy ( v1.begin ( ) , v1.begin ( ) + 8, v1.begin ( ) + 8 );

   cout << "Copying the first half of the vector to the second half\n "
        << "while removing adjacent duplicates gives\n ( " ;
   for ( v1_Iter1 = v1.begin( ) ; v1_Iter1 != v1_NewEnd1 ; v1_Iter1++ )
      cout << *v1_Iter1 << " ";
   cout << ")." << endl;

   int iv;
   for ( iv = 0 ; iv <= 7 ; iv++ )
      v1.push_back( 10 );

   // Remove consecutive duplicates under the binary prediate mod_equals
   v1_NewEnd2 = unique_copy ( v1.begin ( ) , v1.begin ( ) + 14, 
      v1.begin ( ) + 14 , mod_equal );

   cout << "Copying the first half of the vector to the second half\n "
        << " removing adjacent duplicates under mod_equals gives\n ( " ;
   for ( v1_Iter2 = v1.begin( ) ; v1_Iter2 != v1_NewEnd2 ; v1_Iter2++ )
      cout << *v1_Iter2 << " ";
   cout << ")." << endl;
}

Output

Vector v1 is
 ( 5 -5 5 -5 4 4 4 7 10 10 10 10 10 10 ).
Copying the first half of the vector to the second half
 while removing adjacent duplicates gives
 ( 5 -5 5 -5 4 4 4 7 5 -5 5 -5 4 7 ).
Copying the first half of the vector to the second half
  removing adjacent duplicates under mod_equals gives
 ( 5 -5 5 -5 4 4 4 7 5 -5 5 -5 4 7 5 4 7 5 4 7 ).

需求

標題: <algorithm>

命名空間: std

請參閱

參考

標準樣板程式庫