我可以定义一个其键为结构的映射吗?

发布于 2024-09-27 08:04:20 字数 24 浏览 2 评论 0原文

我怎样才能用 C++ 做到这一点?

and how can I do it in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两个我 2024-10-04 08:04:20

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

例如:

struct example { int x; }

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

std::map<example, int> values;

或者,您可以提供一个比较函数作为地图模板的第三个参数,而不是定义operator<。更多详细信息此处(参数Compare)。

You can use any type as a map key, as long as it implements an operator< (plus the usual copy-and-assign requirements for values stored in containers).

For instance:

struct example { int x; }

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

std::map<example, int> values;

Alternatively, you may provide a comparison function as the third argument of the map template instead of defining operator<. More details here (parameter Compare).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文