如何从c ++中的一组向量中删除重复的向量?(how to remove duplicate vectors from a set of vectors in c++?)

系统教程 行业动态 更新时间:2024-06-14 16:52:52
如何从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 ]

更多推荐

本文发布于:2023-04-05 12:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/b5efdf39730422f5508fe0c1984eb1d2.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:向量   remove   duplicate   vectors   set

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!