#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的值。
答案 0 :(得分:0)
#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 ]
答案 1 :(得分:0)
使用一组多重集。 multiset就像一个向量,但是已经排序,因此P和R(转换为多集)将是相等的。
set<multiset<int> > SS;
....
SS.insert(multiset(R.begin(), R.end());
SS.insert(multiset(S.begin(), S.end());
SS.insert(multiset(P.begin(), P.end());