Chinaunix首页 | 论坛 | 博客
  • 博客访问: 440230
  • 博文数量: 116
  • 博客积分: 2756
  • 博客等级: 少校
  • 技术积分: 1163
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-29 21:21
文章分类

全部博文(116)

文章存档

2018年(1)

2017年(1)

2016年(30)

2015年(41)

2014年(23)

2011年(7)

2010年(9)

2008年(4)

分类: C/C++

2015-03-12 15:23:48

代码如下:

点击(此处)折叠或打开

  1. #include <set>
  2. #include <string>
  3. #include <algorithm>
  4. #include <iostream>
  5. int main(void)
  6. {
  7.     using namespace std;
  8.     set<string> set_01;
  9.     set<string> set_02;
  10.     set<string> intersection_tmp;
  11.     set<string> difference_tmp;
  12.     set<string> set_union_tmp;
  13.     set_01.insert("a");
  14.     set_01.insert("b");
  15.     set_01.insert("c");
  16.     set_01.insert("d");
  17.     set_01.insert("e");
  18.     set_02.insert("a");
  19.     set_02.insert("b");
  20.     set_02.insert("c");
  21.     set_02.insert("x");
  22.     set_02.insert("y");

  23.     cout << "Intersection:" << endl;
  24.     
  25.     set_intersection(
  26.         set_01.begin(), set_01.end(),
  27.         set_02.begin(), set_02.end(),
  28.         insert_iterator<set<string>>(intersection_tmp, intersection_tmp.begin()));
  29.     
  30.     for (set<string>::iterator it = intersection_tmp.begin(); it != intersection_tmp.end(); it++)
  31.     {
  32.         cout << *it << endl;
  33.     }
  34.     
  35.     cout << "Difference:" << endl;
  36.     
  37.     set_difference(
  38.         set_01.begin(), set_01.end(),
  39.         intersection_tmp.begin(), intersection_tmp.end(),
  40.         insert_iterator<std::set<std::string>>(difference_tmp, difference_tmp.begin()));
  41.     
  42.     for (set<string>::iterator it = difference_tmp.begin(); it != difference_tmp.end(); it++)
  43.     {
  44.         cout << *it << endl;
  45.     }

  46.     cout << "Union:" << endl;
  47.     
  48.     set_union(
  49.         set_01.begin(), set_01.end(),
  50.         set_02.begin(), set_02.end(),
  51.         insert_iterator<set<string>>(set_union_tmp, set_union_tmp.begin()));
  52.     
  53.     for (set<string>::iterator it = set_union_tmp.begin(); it != set_union_tmp.end(); it++)
  54.     {
  55.         cout << *it << endl;
  56.     }
  57. }
输出如下:

点击(此处)折叠或打开

  1. Intersection:
  2. a
  3. b
  4. c
  5. Difference:
  6. d
  7. e
  8. Union:
  9. a
  10. b
  11. c
  12. d
  13. e
  14. x
  15. y



阅读(2732) | 评论(0) | 转发(0) |
0

上一篇:Vim 自动补齐

下一篇:Debian系统去掉beeper。

给主人留下些什么吧!~~