加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C++ set_intersection(STL set_intersection)用法详解

发布时间:2020-12-16 07:40:16 所属栏目:百科 来源:网络整理
导读:除了会创建两个集合的交集而不是并集之外,set_intersection() 算法的用法和 set_union() 相同。有两个版本的 set_intersection(),它们和 set_union() 拥有相同的参数集。下面的一些语句可以说明它的用法: std::setstring words1 {"one","two","three","fou
除了会创建两个集合的交集而不是并集之外,set_intersection() 算法的用法和 set_union() 相同。有两个版本的 set_intersection(),它们和 set_union() 拥有相同的参数集。下面的一些语句可以说明它的用法:
std::set<string> words1 {"one","two","three","four","five","six"};
std::set<string> words2 {"four","five","six","seven","eight","nine"};
std::set<string> result;
std::set_intersection(std::begin(words1),std::end(words1),std::begin(words2),std::end(words2),std::inserter(result,std::begin(result)));
// Result: "five" "four" "six"
这个 set 容器保存 string 对象,默认使用 less<string> 的实例对元素排序。两个容器中元素的交集是它们共有的元素,它们被保存在 result 容器中。当然,这些元素是升序字符串序列。set_intersection() 算法会返回一个迭代器,它指向目的容器中插入的最后一个元素的下一个位置。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读