在C ++中获取集合的交集,交集或差异 [英] Getting Union, Intersection, or Difference of Sets in C++

查看:149
本文介绍了在C ++中获取集合的交集,交集或差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  1. 有没有办法得到两个C ++集合的联合,交集或区别? (写自己的功能很容易,但我想知道是否有内置的功能)

  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)

可以将C ++集合用作

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


推荐答案

使用 set_difference() set_union() set_intersection() set_symmetric_difference() 功能。

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

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

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.

这篇关于在C ++中获取集合的交集,交集或差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆