gpt4 book ai didi

c++ - 我可以定义一个键是结构的映射吗?

转载 作者:IT老高 更新时间:2023-10-28 22:37:21 25 4
gpt4 key购买 nike

我如何在 C++ 中做到这一点?

最佳答案

您可以使用任何类型作为映射键,只要它实现 operator< (加上对存储在容器中的值的通常复制和分配要求)。

例如:

struct example { int x; }

bool operator < (const example &l, const example &r) { return l.x < r.x; }

std::map<example, int> values;

或者,您可以提供一个比较函数作为 map 模板的第三个参数,而不是定义 operator< .更多详情 here (参数 Compare)。

关于c++ - 我可以定义一个键是结构的映射吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3935681/

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