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
    解决仅向前迭代器的第一个元素的位置在要复制的源范围。

  • _Last
    解决仅向前的迭代器通过最终元素的位置一在要复制的源范围。

  • _Result
    解决输出的迭代器第一个元素的位置在接收的后续复制的目标范围复制移除。

  • _Comp
    定义要满足条件的用户定义的谓词函数对象,如果两个组件要执行视为等效的。 二进制谓词采用两个参数并返回 true,在满足和 false,在未满足。

返回值

解决输出的迭代器通过最终元素的位置一在接收的后续复制的目标范围复制移除。

备注

算法的两种形式中移除第二个重复顺序对相等元素。

算法的操作是稳定的,能够更改已撤消删除元素的相对顺序。

引用的范围必须是有效的;所有指针必须dereferenceable,并在序列中最后位置以访问按增量。

复杂的线性,需要(_Last – _First)进行比较。

unique_copy 有两个相关的窗体:

有关这些功能如何的信息的行为,请参见 经过检查的迭代器

示例

// 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

请参见

参考

标准模板库