unique
C ++標準ライブラリアルゴリズム(を使用)を使用しようとしていますBinaryPredicate
。
ペアのベクトルを作成しました。各ペアは「(first = 4つのdoubleのベクトル、second =整数)」のようなものです。2番目の要素はインデックスとして機能するため、 `uniqueを使用した後でも、元のインデックスを確認できます。
以下の例では、次のようなものを作成しました。
10 20 30 40, 1
10 20 30 40, 2
10 20 30 40, 3
10 20 30 40, 4
10 20 30 40, 5
10 20 30 40, 6
次に、一意の関数を使用して、各ペアの最初の要素のみを比較します。カスタマイズバイナリ予測子を使用しましたuniquepred
。確かに、それは機能しますが、ベクトルは使用後に縮小されませんunique
。
期待される結果
Size before=6
equal!
equal!
equal!
equal!
equal!
Size after=1
実結果
Size before=6
equal!
equal!
equal!
equal!
equal!
Size after=6
最小限の作業例を次に示します。これをデバッグするのを手伝ってください。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef std::vector<double> V1db;
typedef std::pair<V1db, int > Pairs;
typedef std::vector<Pairs> Vpairs;
bool uniquepred( const Pairs& l, const Pairs& r) {
if (l.first==r.first)
cout<<"equal!"<<endl;
return l.first == r.first;
}
int main()
{
Vpairs ak;
V1db u2(4);
u2[0]=10;u2[1]=20;u2[2]=30;u2[3]=40;
Pairs m2;
m2.first = u2;
m2.second= 1;
ak.push_back(m2);
m2.second= 2;
ak.push_back(m2);
m2.second= 3;
ak.push_back(m2);
m2.second= 4;
ak.push_back(m2);
m2.second= 5;
ak.push_back(m2);
m2.second= 6;
ak.push_back(m2);
cout<<"Size before="<<ak.size()<<endl;
unique(ak.begin(), ak.end(), uniquepred);
cout<<"Size after="<<ak.size()<<endl;
return 0;
}