在 C++ 中获取集合的并集、交集或差集

发布于 2024-08-11 01:20:51 字数 161 浏览 3 评论 0原文

我有几个关于如何使用 C++ 集 (std::set) 的问题

  1. 有没有办法获得两个 C++ 集的并集、交集或差集? (编写我自己的函数来做到这一点非常容易,但我想知道是否有内置函数)

  2. C++ 集合可以用作映射中的键吗?

I have a couple questions about how to use C++ sets (std::set)

  1. Is there a way to get the union, intersection, or difference of two C++ sets? (It's pretty easy to write my own functionto do that but I wanted to know if there was a built in function for it)

  2. Can C++ sets be used as keys in a map?

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

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

发布评论

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

评论(2

疯狂的代价 2024-08-18 01:20:51

使用set_difference()set_union(), set_intersection()set_symmetry_difference() 函数。

集合和映射支持任何可以比较的键类型。默认情况下,这意味着该类型已定义operator<(),但您可以提供自己的比较器。 C++ 集没有定义operator<(),因此不能用作键,除非您提供自己的比较器。

Use the set_difference(), set_union(), set_intersection() and set_symmetric_difference() functions.

Sets and maps support any key type that can compare. By default this means the type has operator<() defined, but you can provide your own comparator. C++ sets don't have operator<() defined and therefore can't be used as keys unless you provide your own comparator.

薄情伤 2024-08-18 01:20:51

只要您提供可以比较它们的类或函数,任何东西都可以用作映射中的键。 这里是一个示例。

Anything can be used as a key in a map as long as you provide a class or function that can compare them. Here is an example.

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