gpt4 book ai didi

c++ - 使用类类型作为映射键值

转载 作者:太空狗 更新时间:2023-10-29 20:07:24 24 4
gpt4 key购买 nike

Data 变量可以用作映射键吗?

struct Data {
Data(int X, int Y) {x=X; y=Y;}
int x; int y;
}

int main()
{
std::map<Data, int> map_;
map_.insert(std::make_pair(Data(1,2), 0)); //error inserting
}

最佳答案

是的,但您要么需要定义 operator<对于类类型或对 std::map 使用自定义比较函数.

There is an example of using a custom comparison function in the STL documentation.

关于c++ - 使用类类型作为映射键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3102580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com