私は、ある関数に値渡しするポインタのベクトルを消去しようとしています。値を渡す理由は、関数への多数の呼び出しでこれらの値を消去することを計画しているからです。だから私がポインタ/参照を渡す場合、私はこれを達成できませんでした。ポインタのベクトルを渡して重複を削除する
まずは上記の文は正しいですか?ここで
は、いくつかのサンプルコードです:
vector<Boson*>* BosonMaker::remove_duplicates(vector<Boson*>* boson_candidates, vector<Particle*> child_candidates){
vector<Particle*> used_leptons.clear();
// This needs deleting at some point
m_unduplicated_bosons = new vector<Boson*>();
for(int i_b = 0; boson_candidates->size(); i_b++){
vector<Particle*>::iterator child1_finder = find(used_leptons.begin(), used_leptons.end(), boson_candidates->at(i_b)->Child1());
//Search pointer will reach end of collection if child isn't in the used_leptons vector
if (child1_finder == used_leptons.end()) {
vector<Particle*>::iterator child2_finder = find(used_leptons.begin(), used_leptons.end(), boson_candidates->at(i_b)->Child2());
if (child2_finder == used_leptons.end()) {
used_leptons.push_back(boson_candidates->at(i_b)->Child1());
used_leptons.push_back(boson_candidates->at(i_b)->Child2());
// And add the boson to the vector of final bosons
unduplicated_bosons->push_back(boson_candidates->at(i_b));
}
}
}
// Now make a vector of unused leptons
for (int i_l = 0; i_l < used_leptons.size(); i_l++) {
vector<Particle*>::iterator lepton_finder = find(child_candidates.begin(), child_candidates.end(), used_leptons.at(i_l));
child_candidates.erase(lepton_finder);
}
return unduplicated_bosons;
}
私はその後、私の質問があるので、
vector<Boson*> *m_boson_finals_elpair = remove_duplicates(&m_boson_electronPair_candidates, m_all_particle_candidates);
vector<Boson*> *m_boson_finals_mupair = remove_duplicates(&m_boson_muonPair_candidates, m_all_particle_candidates);
vector<Boson*> *m_boson_finals_elneutrino = remove_duplicates(&m_boson_electronNeutrino_candidates, m_all_particle_candidates);
vector<Boson*> *m_boson_finals_muneutrino = remove_duplicates(&m_boson_muonNeutrino_candidates, m_all_particle_candidates);
のようにクラス内でこのメンバ関数を使用します。
あるうとm_all_particle_candidates
vector<Particle*> m_all_particle_candidates;
はremove_duplicatesの呼び出しごとに異なっていますか?
私は値渡しのため、実際のオブジェクトではなくベクトルから消去されたイテレータlepton_finderを質問しようとしていますか?
注:remove_duplicate関数にタイプミスがありました。私はポインタではなく値を渡した。それは値でなければなりません
私はここであなたの質問に答えました:http://stackoverflow.com/questions/1041620/most-efficient-way-to-erase-duplicates-and-sort-ac-vector/14844222#14844222 ptgi: :メモリリークを避けるためのstd :: unique()の置き換え、重複した削除、および分割の失敗を記述するunique()記事。 -dbednar ahd 310 – joe