C++ 在vector中删除重复项的最简单方法是什么<;配对<;A、 A>&燃气轮机;如果{x,y}和{y,x}也被认为是相同的?
如果两对{x,y}和{y,x}也被认为是重复的,是否有内置的方法来删除对向量的重复项 例如,如果我有类似的向量:C++ 在vector中删除重复项的最简单方法是什么<;配对<;A、 A>&燃气轮机;如果{x,y}和{y,x}也被认为是相同的?,c++,vector,std-pair,C++,Vector,Std Pair,如果两对{x,y}和{y,x}也被认为是重复的,是否有内置的方法来删除对向量的重复项 例如,如果我有类似的向量: {{1,2},{4,3},{2,1},{5,6},{1,2},{3,4},{0,1}} 我想删除副本,使其成为: {{1,2},{4,3},{5,6},{0,1}} 是否有任何内置函数来处理{x,y}和{y,x}相同的情况 如果没有,最简单的方法是什么 我曾考虑使用类似的for循环,但不起作用: vector<int> isRemove; int count=0; f
{{1,2},{4,3},{2,1},{5,6},{1,2},{3,4},{0,1}}
我想删除副本,使其成为:
{{1,2},{4,3},{5,6},{0,1}}
是否有任何内置函数来处理{x,y}和{y,x}相同的情况
如果没有,最简单的方法是什么
我曾考虑使用类似的for循环,但不起作用:
vector<int> isRemove;
int count=0;
for(pair<int,int> a : p){
isRemove.push_back(0);
for(pair<int,int> b : p){
if((a.first==b.first && a.second==b.second) || (a.first==b.second && a.second==b.first)){
isRemove[count]=1;
break;
}
}
count++;
}
for(int i=isRemove.size()-1;i>=0;i--){
printf("%d\n",isRemove[i]);
if(isRemove[i]){
p.erase(p.begin()+i);
}
}
向量isRemove;
整数计数=0;
用于(a:p对){
isRemove.push_back(0);
用于(b:p对){
如果((a.first==b.first&&a.second==b.second)| |(a.first==b.second&&a.second==b.first)){
isRemove[计数]=1;
打破
}
}
计数++;
}
对于(int i=isRemove.size()-1;i>=0;i--){
printf(“%d\n”,isRemove[i]);
if(isRemove[i]){
p、 擦除(p.begin()+i);
}
}
还有其他更简单的方法吗?保存唯一的值。唯一性由一个参数决定。您可以按如下方式实现所需的解决方案():
您只需定义一个带有自定义比较器的集合,该比较器在调用std::less
时确保每对中的顺序一致,然后填充集合
#include <vector>
#include <algorithm>
#include <iostream>
int main(){
using namespace std;
using Intpair = std::pair<int,int>;
vector<Intpair> v = {{1,2},{4,3},{2,1},{5,6},{1,2},{3,4},{0,1}};
//Normalize == sort the pair members
for(auto& p : v){
int x = max(p.first, p.second), y = min(p.first, p.second);
p.first = x; p.second = y;
}
//Sort the pairs
sort(v.begin(), v.end(),[](Intpair x, Intpair y){ return (x1 < y1) || (x1==y1 && x2<y2); } );
//Print the vector in its normalized and sorted form
for(auto p : v){ cout<<p.first<<' '<<p.second<<'\n'; }
cout<<'\n';
//Unique the vector
auto last = unique(v.begin(), v.end() );
v.erase(last, v.end());
//Print the unique'd vector
for(auto p : v){ cout<<p.first<<' '<<p.second<<'\n'; }
}
对于较小的向量,这应该比您使用
std::set
获得的性能更好,因为后者不像向量那样对缓存友好。以下程序将完全满足您的需要:
#include <iostream>
#include <vector>
int main () {
std::vector<std::pair<int, int>> myvector;
myvector.push_back(std::make_pair(1,2));
myvector.push_back(std::make_pair(4,3));
myvector.push_back(std::make_pair(2,1));
myvector.push_back(std::make_pair(5,6));
myvector.push_back(std::make_pair(1,2));
myvector.push_back(std::make_pair(3,4));
myvector.push_back(std::make_pair(0,1));
auto it = myvector.begin();
for (; it != myvector.end(); ++it) {
auto pit = myvector.begin();
for (; pit != myvector.end();) {
if (((it->first == pit->first && it->second == pit->second) || (it->first == pit->second && it->second == pit->first)) && (pit != it)) {
std::cout << "found pair " << it->first << "," << it->second << " with " << pit->first << "," << pit->second << std::endl;
pit = myvector.erase(pit);
} else {
++pit;
}
}
}
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << it->first << "," << it->second;
std::cout << '\n';
return 0;
}
-使用自定义条件。简单的方法是使用带有自定义比较器的
std::set
,这样您就不会在一开始就得到重复项。是否有特别的原因需要使用向量@LuchianGrigore没有涵盖向量不同元素的比较,是吗?boost库为此提供了一些实用程序。使用起来也很方便。你的比较函数应该有一个const
调用操作符。@JonathanWakely当然应该有。我来解决。你的比较函数允许astd::minmax
来实现这一点,很高兴找到新的东西!:)
#include <vector>
#include <algorithm>
#include <iostream>
int main(){
using namespace std;
using Intpair = std::pair<int,int>;
vector<Intpair> v = {{1,2},{4,3},{2,1},{5,6},{1,2},{3,4},{0,1}};
//Normalize == sort the pair members
for(auto& p : v){
int x = max(p.first, p.second), y = min(p.first, p.second);
p.first = x; p.second = y;
}
//Sort the pairs
sort(v.begin(), v.end(),[](Intpair x, Intpair y){ return (x1 < y1) || (x1==y1 && x2<y2); } );
//Print the vector in its normalized and sorted form
for(auto p : v){ cout<<p.first<<' '<<p.second<<'\n'; }
cout<<'\n';
//Unique the vector
auto last = unique(v.begin(), v.end() );
v.erase(last, v.end());
//Print the unique'd vector
for(auto p : v){ cout<<p.first<<' '<<p.second<<'\n'; }
}
1 0
2 1
2 1
2 1
4 3
4 3
6 5
1 0
2 1
4 3
6 5
#include <iostream>
#include <vector>
int main () {
std::vector<std::pair<int, int>> myvector;
myvector.push_back(std::make_pair(1,2));
myvector.push_back(std::make_pair(4,3));
myvector.push_back(std::make_pair(2,1));
myvector.push_back(std::make_pair(5,6));
myvector.push_back(std::make_pair(1,2));
myvector.push_back(std::make_pair(3,4));
myvector.push_back(std::make_pair(0,1));
auto it = myvector.begin();
for (; it != myvector.end(); ++it) {
auto pit = myvector.begin();
for (; pit != myvector.end();) {
if (((it->first == pit->first && it->second == pit->second) || (it->first == pit->second && it->second == pit->first)) && (pit != it)) {
std::cout << "found pair " << it->first << "," << it->second << " with " << pit->first << "," << pit->second << std::endl;
pit = myvector.erase(pit);
} else {
++pit;
}
}
}
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << it->first << "," << it->second;
std::cout << '\n';
return 0;
}
myvector contains: 1,2 4,3 5,6 0,1