假设你有一个类:
class SomeClass{
public:
int x;
SomeClass(){
x = rand();
}
bool operator<(const SomeClass& rhs) const{
return x < rhs.x;
}
};
然后你有这个:
map<SomeClass, string> yeah;
显然这会起作用:
yeah[SomeClass()] = "woot";
但是有没有办法得到这样的东西:
yeah[3] = "huh";
工作?我的意思是,除了其他运算符之外,我还尝试设置 operator<(int rhs) ,但没有骰子。这可能吗?
最佳答案
添加构造函数:
SomeClass(int y){
x = y;
}
关于c++ - 使用与索引不同的类访问 C++ 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5451096/