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
コピーされるソース範囲内の最後の要素の一つ前の位置 1 に対処前方反復子。_Result
削除された実行中の複製を持つコピーを受信する先の範囲の先頭の要素の位置を示す出力反復子。_Comp
2 個の要素が同じ値として取得する場合に満たされている必要条件を定義するユーザー定義の述語関数オブジェクト。バイナリ述語が満たされなかった場合に完了したら 2 個の引数を受け取り、true と false を返します。
戻り値
削除された実行中の複製を持つコピーを受信する先の範囲の最後の要素の一つ前の位置 1 を示す出力反復子。
解説
アルゴリズムのフォームは、実行中のペアの等値要素の 2 番目の複製を削除します。
アルゴリズムの操作は残された要素の相対位置ディレクティブが変更されないように安定しています。
参照される範囲が有効である必要があります; すべてのポインターが dereferenceable なり、シーケンス内で最後の位置は incrementation によって最初からアクセスできます。
複雑度は、線形で、(_Last – _First) 比較が必要です。
unique_copy 2 に関連する二つの形式があります:
ついては、これらの関数がどのようにするか、チェックを行う反復子を参照してください。
使用例
// 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;
}
出力
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