码迷,mamicode.com
首页 > 其他好文 > 详细

STL 集合部分操作

时间:2018-03-27 21:59:18      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:intersect   tin   size   方法   cout   tor   iostream   参数   ret   

set_intersection

default (1)
template <class InputIterator1, class InputIterator2, class OutputIterator>
  OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
                                   InputIterator2 first2, InputIterator2 last2,
                                   OutputIterator result);
custom (2)
template <class InputIterator1, class InputIterator2,
          class OutputIterator, class Compare>
  OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
                                   InputIterator2 first2, InputIterator2 last2,
                                   OutputIterator result, Compare comp);

求两个(已排序)集合的交集。

构造一个排序的范围,从result的位置开始,然后是两个排序范围的集合的交集[first1,last1]和[first2,last2)。

两个集合的交集只由两个集合中的元素组成。由函数复制的元素总是以相同的顺序从第一个范围开始。

默认升序排列,也可以用comp自定义。两个元素,a和b被认为是相等的,如果(!(a<b) && !(b<a)或if (!comp(a,b) && !comp(b,a))。

范围内的元素已经按照相同的标准(操作符<或comp)排序。结果的范围也根据这个排序。

 

1.     前四个参数,可以是迭代器(vector,set)的首尾,也可以是数组的首尾,只要是有序的区间均可注意区间均为左闭右开。

2.  第五个参数,可以是vector(注意一定要预留大小!用it取得set_intersection的返回值后再resize),也可以是数组。

3.  compare函数自己编写

 1 // set_intersection example
 2 #include <iostream>     // std::cout
 3 #include <algorithm>    // std::set_intersection, std::sort
 4 #include <vector>       // std::vector
 5 
 6 int main () {
 7   int first[] = {5,10,15,20,25};
 8   int second[] = {50,40,30,20,10};
 9   std::vector<int> v(10);                      // 0  0  0  0  0  0  0  0  0  0
10   std::vector<int>::iterator it;
11 
12   std::sort (first,first+5);     //  5 10 15 20 25
13   std::sort (second,second+5);   // 10 20 30 40 50
14 
15   it=std::set_intersection (first, first+5, second, second+5, v.begin());
16                                                // 10 20 0  0  0  0  0  0  0  0
17   v.resize(it-v.begin());                      // 10 20
18 
19   std::cout << "The intersection has " << (v.size()) << " elements:\n";
20   for (it=v.begin(); it!=v.end(); ++it)
21     std::cout <<   << *it;
22   std::cout << \n;
23 
24   return 0;
25 }

set_union

求并集,方法和注意点与上面相同

set_difference

求差集,同上

merge

 1 // merge algorithm example
 2 #include <iostream>     // std::cout
 3 #include <algorithm>    // std::merge, std::sort
 4 #include <vector>       // std::vector
 5 
 6 int main () {
 7   int first[] = {5,10,15,20,25};
 8   int second[] = {50,40,30,20,10};
 9   std::vector<int> v(10);
10 
11   std::sort (first,first+5);
12   std::sort (second,second+5);
13   std::merge (first,first+5,second,second+5,v.begin());
14 
15   std::cout << "The resulting vector contains:";
16   for (std::vector<int>::iterator it=v.begin(); it!=v.end(); ++it)
17     std::cout <<   << *it;
18   std::cout << \n;
19 
20   return 0;
21 }

合并两个有序区间

STL 集合部分操作

标签:intersect   tin   size   方法   cout   tor   iostream   参数   ret   

原文地址:https://www.cnblogs.com/Jiiiin/p/8660205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!