i'm trying remove duplicated item bigger vector
ex:
6 11 7 8 6 16 17
i should get:
6 11 7 8 16 17
what have:
vector<vector<int>>b; vector<vector<int>>::iterator b_list; vector<vector<int>>::iterator b_it; vector<int>::iterator b_list_it; vector<int>::iterator b_it_it; (b_list = b.begin(); b_list != b.end()-1; ++b_list) { (b_it = b_list+1; b_it != b.end(); ++b_it) { (int = 0; < (*b_list).size(); ++i) { (int j = 0; j < (*b_it).size(); ++j) { if ((*b_list)[i] == (*b_it)[j]) { if ((*b_list).size() > (*b_it).size()) { (*b_list).erase((*b_list).begin()); } if ((*b_list).size() < (*b_it).size()) { (*b_it).erase((*b_it).begin()); } } } } } }
in case erase() doesn't remove anything. why so? , used instead of it?
thank you!
since op has 2 dimensional vector. not sure if op wants data in sorted (unique can used in case) taking assumption not sorted set used tracker . keeps track of unique values . if (ret.second!=false)
fogrow.push_back(s)
;a copy vector used every time unique values there pushed in
// // main.cpp // ranged // // created hariom singh on 9/9/17. // copyright © 2017 hariom singh. rights reserved. // #include <iostream> #include <string> #include <vector> #include <set> int main() { std::vector<std::vector<int> > val {{6,11},{7,8},{6,16,17}}; std::vector<std::vector<int> > uniquevalcopy; std::set<int> tracker; ( const auto &row : val ) { std::vector <int> fogrow; ( const auto &s : row ) { std::cout << s << ' '; auto ret = tracker.insert(s); if (ret.second!=false) fogrow.push_back(s); } uniquevalcopy.push_back(fogrow); std::cout << std::endl; } std::cout<<"after removal"<<"\n"; ( const auto &row : uniquevalcopy ) { ( const auto &s : row ) { std::cout << s << ' '; } std::cout << std::endl; } return 0; }
output
6 11 7 8 6 16 17 after removal 6 11 7 8 16 17 program ended exit code: 0
Comments
Post a Comment