【发布时间】:2020-04-13 06:13:04
【问题描述】:
我有一个std::map,它的键是另一个类。像下面的例子:
class KeyClass
{
public:
int a;
int b;
};
main(){
//some code
std::map<KeyClass, SomeOtherClass> mapVariable;
KeyClass k1(); k1.a = 1; k1.b = 1;
KeyClass k2(); k2.a = 2; k2.b = 2;
mapVariable[k1] = SomeOtherClass();
mapVariable[k2] = SomeOtherClass();
//I am trying something like see if any element is there whose key.a = 1 (or may be key.a = 6 --> will fail here)?
}
我尝试遍历mapVariable 并检查iter->first.a == 1,但无论如何我可以使用map::count() 函数来获得它吗?
我在 CLI C++ 代码中使用它,所以我似乎无法使用 lambda 函数。
【问题讨论】:
-
仅供参考,您的
KeyClass需要定义一个operator<才能用作std::map键。