set::find()找不到集合中的键
keys in the set cannot be found by set::find()
我在用set
做一些实验时遇到了这个问题。
我使用一个以2个整数为密钥的结构:
struct Key {
int k1;
int k2;
};
并使用一个类来构建密钥之间的排序:
struct keyComp {
bool operator () (const struct Key& lhs, const struct Key& rhs) const {
if (lhs.k1 < rhs.k1)
return true;
else if (lhs.k2 < rhs.k2)
return true;
else
return false;
}
};
但有了这个比较器,该集就没能找到一些现有的密钥。例如,在这个程序中,我在集合中存储了9个密钥,从(0, 0)
到(2, 2)
:
Key pos;
set <Key, keyComp> aset;
// insert elements from (0, 0) to (2, 2)
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
pos.k1 = i;
pos.k2 = j;
aset.insert (pos);
}
}
// now try to find each of them
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
pos.k1 = i;
pos.k2 = j;
set <Key, keyComp> :: iterator it = aset.find (pos);
if (it != aset.end ())
cout << "v "; // element found
else
cout << "! "; // not found
}
cout << endl;
}
// print the set
cout << "element in set : ";
for (set <Key, keyComp> :: iterator it = aset.begin (); it != aset.end (); it++) {
cout << "(" << it->k1 << ", " << it->k2 << ") ";
}
cout << endl;
我预计它会打印9个v
,这意味着所有的密钥都找到了。但我得到了:
v v v
! ! v
! ! v
element in set : (0, 0) (1, 0) (2, 0) (0, 1) (1, 1) (2, 1) (0, 2) (1, 2) (2, 2)
有些钥匙可以找到,但有些却找不到,甚至它们都在套装中。
此外,如果我将比较器更改为:
struct keyComp {
bool operator () (const struct Key& lhs, const struct Key& rhs) const {
// combine the two keys for comparison
// say LARGE_NUMBER is a number bigger than all k2
return lhs.k1 * LARGE_NUMBER + lhs.k2 < rhs.k1 * LARGE_NUMBER + rhs.k2;
}
};
然后找到所有钥匙。
为什么会发生这种情况?是因为原始比较器未能构造键之间的顺序吗?
您的比较器没有提供正确的排序,这意味着set
的内部在试图找出插入或查找内容的位置时会做各种奇怪的事情(也称为"未定义行为")。
你需要这样的东西:
if (lhs.k1 < rhs.k1)
return true;
if (lhs.k1 > rhs.k1)
return false;
return (lhs.k2 < rhs.k2);
您的比较器没有定义严格的弱排序。(例如,在比较器中,(2,1)
和(1,2)
在两个方向上给出true
。)您需要类似字典排序的东西:
return (lhs.k1 < rhs.k1) || ( !(rhs.k1 < lhs.k1) && (lhs.k2 < rhs.k2) );
相关文章:
- 处理多个异常集合的C++方法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- 如何将ampl中的集合表示为c++中的向量
- 检查值是否在集合p1和p2中,但不在p3中
- 带过滤器的现代迭代c++集合
- 如何将结构插入到集合中并打印集合的成员
- 集合上的输出迭代器:assign和increment迭代器
- 如何将函数集合传递给客户端类,以便将它们当作客户端类本身的成员使用
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 实现一个在集合上迭代的模板函数
- 尝试使用集合函数时出现分段错误
- 互斥指针的集合
- c++找不到具有相同哈希的无序集合元素
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- python集合的C++等价物是什么.计数器
- 为什么 std::find( s.begin(), s.end(), val ) 比集合 s 的 s.find(val) 慢 1000 倍<int>?
- find() 函数对集合有效吗?
- set::find()找不到集合中的键
- set::find 和 std::find 都无法在一个集合中找到一些双元素