当类包含 2D 数组时,如何将类用作映射的键
How to use a class as a key of a map when the class contains a 2d array?
>我有一个看起来像下面的类:
class A{
private:
int *a[10];
};
现在我想要一张地图,它将以提到的类为关键。
map<A,int> visited;
如何在此处重载较少运算符/编写比较函数,以便地图可以识别重复的 2D 数组?我在类中写了一个重载器。但是,它将包含重复数组的对象视为不同的对象。这是我编写的函数:
bool operator<(const A& other) const{
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(a[i][j]!=other.a[i][j])return true;
}
}
return false;
}
我在代码中找不到问题。有人可以帮忙吗?
bool operator<(const A& other) const{
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(a[i][j]==other.a[i][j]) continue;
return a[i][j]<other.a[i][j];
}
}
return false;
}
这应该适用于地图。但如果阵列很大,它可能会很慢。考虑编写哈希函数并使用unordered_map。
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 如何加载(或映射)文件部分的最大大小,但适合在Windows上的RAM
- 如何使用用户输入在C++中正确填充2D数组
- 当类包含 2D 数组时,如何将类用作映射的键
- 将2D图像映射到3D纹理
- C++2D矢量映射
- 内存映射2D阵列使用boost
- 2d(3d)坐标的哈希映射(即双精度向量)
- 2D纹理或立方体映射
- 2D数组可以作为映射的键吗?
- 将2D数组映射到CUDA中的RGB到GreyScale程序中的块网格
- 2D 数组映射与 if 语句 C++
- 如何在 C++ 中映射 1D 数组中的 2D 数组