如何从c ++中的一组向量中删除重复的向量?(how to remove duplicate vectors from a set of vectors in c++?)
#include <iostream>
#include <cstdio>
#include <set>
#include <vector>
using namespace std;
int main() {
vector<int> S, P, R;
set<vector<int> > SS;
S.push_back(9);
S.push_back(9);
S.push_back(9);
P.push_back(656);
P.push_back(11);
P.push_back(10);
R.push_back(10);
R.push_back(11);
R.push_back(656);
SS.insert(R);
SS.insert(S);
SS.insert(P);
set<vector<int> >::iterator itr;
vector<int>::iterator i;
for(itr = SS.begin(); itr != SS.end(); itr++) {
for(i = itr -> begin(); i != itr -> end(); i++) {
cout<<*i<<" ";
}
}
return 0;
}
应显示向量S和P的值或向量R和S.不能使用set<set<int> >使用,因为它将拆除向量S的值。
#include <iostream> #include <cstdio> #include <set> #include <vector> using namespace std; int main() { vector<int> S, P, R; set<vector<int> > SS; S.push_back(9); S.push_back(9); S.push_back(9); P.push_back(656); P.push_back(11); P.push_back(10); R.push_back(10); R.push_back(11); R.push_back(656); SS.insert(R); SS.insert(S); SS.insert(P); set<vector<int> >::iterator itr; vector<int>::iterator i; for(itr = SS.begin(); itr != SS.end(); itr++) { for(i = itr -> begin(); i != itr -> end(); i++) { cout<<*i<<" "; } } return 0; }Values of vector S and P should be displayed or vector R and S. Use of set<set<int> > cannot be used as it will demolish values of vector S.
最满意答案
#include <iostream> #include <vector> #include <algorithm> #include <functional> using initial_vector = std::vector<int>; using vector_of_vecs = std::vector<initial_vector>; template<class...Args> vector_of_vecs uniqify(Args&&...args) { // build a temporary vector of vectors vector_of_vecs result { args... }; // sort it. std::unique requires the vector to be sorted sort(result.begin(), result.end()); // erase all items that are not unique. result.erase(std::unique(result.begin(), result.end()), result.end()); // return the result return result; } auto main() -> int { using namespace std; vector<int> A, B, C; A = {2, 4, 5}; B = {2, 2, 2}; C = {2, 4, 5}; // note: to avoid copies, simply move the vectors into uniquify auto uv = uniqify(std::move(A), std::move(B), std::move(C)); // or preserve the source vectors and force copies if you wish //auto uv = uniqify(A, B, C); // uv is now a vector of unique vectors. // print out our vector of unique vectors for(const auto& i : uv){ cout << '['; auto sep = " "; for (const auto& j : i) { cout << sep << j; sep = ", "; } cout << " ]\n"; } return 0; }预期结果:
[ 2, 2, 2 ] [ 2, 4, 5 ] #include <iostream> #include <vector> #include <algorithm> #include <functional> using initial_vector = std::vector<int>; using vector_of_vecs = std::vector<initial_vector>; template<class...Args> vector_of_vecs uniqify(Args&&...args) { // build a temporary vector of vectors vector_of_vecs result { args... }; // sort it. std::unique requires the vector to be sorted sort(result.begin(), result.end()); // erase all items that are not unique. result.erase(std::unique(result.begin(), result.end()), result.end()); // return the result return result; } auto main() -> int { using namespace std; vector<int> A, B, C; A = {2, 4, 5}; B = {2, 2, 2}; C = {2, 4, 5}; // note: to avoid copies, simply move the vectors into uniquify auto uv = uniqify(std::move(A), std::move(B), std::move(C)); // or preserve the source vectors and force copies if you wish //auto uv = uniqify(A, B, C); // uv is now a vector of unique vectors. // print out our vector of unique vectors for(const auto& i : uv){ cout << '['; auto sep = " "; for (const auto& j : i) { cout << sep << j; sep = ", "; } cout << " ]\n"; } return 0; }expected result:
[ 2, 2, 2 ] [ 2, 4, 5 ]更多推荐
发布评论