具有反向相同数字的向量
vector of vector having reverse same numbers
如果我有一个向量向量,我们称之为:
vector<vector<int> > data;
在数据中,它有这样的数字
0 1
0 3
0 4
1 0
1 2
1 5
3 0
我怎样才能摆脱与自身相反的数据?例如:0 1 和 1 0,我想摆脱 1 0,因为我已经看到了 0 1。另一个例子:0 3 和 3 0,我想摆脱 3 0,因为我已经看到了 0 3。
所以数据将是这样的:
0 1
0 3
0 4
1 2
1 5
最简单的方法是什么?
您可以在集合中推送向量,并检查集合中是否已经存在反向向量。像这样:
C++11版本:
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
vector<int> myrev(vector<int>& f)
{
vector<int> s;
s.push_back(f[1]);
s.push_back(f[0]);
return s;
}
int main()
{
vector<vector<int> > data={{0,1},{0,3},{0,4},{1,0},{1,2},{1,5},{3,0},{1,0}};
set<vector<int> > unique_data;
for(auto& x: data)
{
if(unique_data.find(myrev(x))==unique_data.end())
unique_data.insert(x);
}
for(auto& x: unique_data)
{
cout << x[0] << ":" << x[1] << endl;
}
return 0;
}
C++98版本:
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
vector<int> myrev(vector<int>& f)
{
vector<int> s;
s.push_back(f[1]);
s.push_back(f[0]);
return s;
}
int main()
{
vector<vector<int> > data;
//lame C++98 initialization of the vector
vector<int> tmp(2);
tmp[0]=0;tmp[1]=1;
data.push_back(tmp);
tmp[0]=0;tmp[1]=3;
data.push_back(tmp);
tmp[0]=0;tmp[1]=4;
data.push_back(tmp);
tmp[0]=1;tmp[1]=0;
data.push_back(tmp);
tmp[0]=1;tmp[1]=2;
data.push_back(tmp);
tmp[0]=1;tmp[1]=5;
data.push_back(tmp);
tmp[0]=3;tmp[1]=0;
data.push_back(tmp);
set<vector<int> > unique_data;
for(vector<vector<int> >::iterator x=data.begin(); x!=data.end(); x++)
{
if(unique_data.find(myrev(*x))==unique_data.end())
unique_data.insert(*x);
}
for(set<vector<int> >::iterator x=unique_data.begin(); x!=unique_data.end(); x++)
{
cout << (*x)[0] << ":" << (*x)[1] << endl;
}
return 0;
}
由于您可能希望打印出没有相反值的值,因此您可以这样做:
for each pair:
if it exists in the HashMap:
do nothing
else
add the opposite to a HashMap
print the pair
如果您能够负担得起使用大量内存,并且整数的最大大小像您的示例一样很小,那么我将简单地创建一个足够大的位向量来容纳整个搜索空间。 从两个输入数字计算到此位向量的索引。
int N_POSSIBLE_PAIRS = (1 << MAX_BITS) * (1 << MAX_BITS);
// vector<bool> is specialized - it only uses 1 bit per entry
std::vector<bool> bitset(N_POSSIBLE_PAIRS);
int index = (first << MAX_BITS) | second;
// in a loop,
if (bitset[index]) {
// duplicate
}
else {
int reverse_index = (second << MAX_BITS) | first;
bitset[index] = true;
bitset[reverse_index] = true;
}
这实际上浪费了 2 倍的空间 - 如有必要,您可以使用更复杂的索引方案来修复它。
如果整数的最大大小太大,或者您的内存受到限制,或者您只是喜欢节俭内存,那么我会按字典顺序对对进行排序并使用二叉搜索来检查重复项。
我的建议也可能在稀疏数据上表现不佳,因为它对缓存不友好。
试试这个(假设列表中没有负数,并且出于空间原因,数字不会太大):
1) 创建一个大小为 MxM 的位集 2D 网格(其中 M 是预期的最大数字)。 将网格中的每个位设置为 0
2) 对于每对数字 (x,y):
check if grid(x,y) is 1. If yes then you have a duplicate
else
check if grid(y,x) is 1. If yes then you have a duplicate.
set grid(x,y) and grid(y,x) to 1
相关文章:
- 用C++中的数字和条件填充向量
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 如何从向量或数组中选择最常见的数字?(前五名)C++
- 输入验证将数字放入向量中
- 如何在向量中某个数字之后重新计算进程
- 如何使用count_if计算向量中的可变数字范围
- 删除C++中向量中第一个最小的数字(并保持顺序)
- 如何将数字转换为整数向量?
- 如何在两个不同大小的向量中找到公共数字
- 如果我们从每个 Y 向量中选择一个值,则 X 数字的每个组合都可能
- Rcpp - 用二进制函数转换数字向量?
- 从数字向量生成随机数
- 比较两个排序的数字向量(性能优先)
- 如何有效地将数字矢量列表组合到一个大数字向量中
- 如何引用数字向量
- 将二进制整数的向量转换为数字向量
- 有效地将数字向量的每个元素与前一个元素进行比较
- 在Rcpp中分配一个大型的二维数字向量数组
- 将数字向量从基数转换为基数