集合

使用set或multiset之前,必须加入头文件<set>

Set、multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素。

sets和multiset内部以平衡二叉树实现

1.   常用函数

1)        构造函数和析构函数

set c:创建空集合,不包含任何元素

set c(op):以op为排序准则,产生一个空的set

set c1(c2):复制c2中的元素到c1中

set c(const value_type *first, const value_type* last):复制[first, last)之间元素构成新集合

set c(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新集合。

c.~set()销毁所有元素,释放内存

multiset mc:创建空集合,不包含任何元素

multiset mc(op):以op为排序准则,产生一个空的set

multiset c1(c2):复制c2中的元素到c1中

multiset c(const value_type *first, const value_type* last):复制[first, last)之间元素构成新集合

multiset c(const value_type *first, const value_type* last,op):以op为排序准则,复制[first, last)之间元素构成新集合。

c.~set()销毁所有元素,释放内存

[cpp] view
plain
 copy

  1. // constructing sets
  2. #include <iostream>
  3. #include <set>
  4. bool fncomp (int lhs, int rhs) {return lhs<rhs;}
  5. struct classcomp {
  6. bool operator() (const int& lhs, const int& rhs) const
  7. {return lhs<rhs;}
  8. };
  9. int main ()
  10. {
  11. std::set<int> first;                           // empty set of ints
  12. int myints[]= {10,20,30,40,50};
  13. std::set<int> second (myints,myints+5);        // range
  14. std::set<int> third (second);                  // a copy of second
  15. std::set<int> fourth (second.begin(), second.end());  // iterator ctor.
  16. std::set<int,classcomp> fifth;                 // class as Compare
  17. bool(*fn_pt)(int,int) = fncomp;
  18. std::set<int,bool(*)(int,int)> sixth (fn_pt);  // function pointer as Compare
  19. return 0;
  20. }

2)        大小、判断空函数

int size() const:返回容器元素个数

    bool empty() const:判断容器是否为空,若返回true,表明容器已空

3)        增加、删除函数

pair<iterator,bool> insert( x):插入元素x

iterator insert(iterator it,x):在迭代器it处插入元素x

void insert(const value_type *first,const value_type *last):插入[first, last)之间元素

iterator erase(iterator it):删除迭代器指针it处元素

iterator erase(iterator first,iterator last):删除[first, last)之间元素

size_type erase(const Key& key):删除元素值等于key的元素

[cpp] view
plain
 copy

  1. #include <iostream>
  2. #include <set>
  3. int main ()
  4. {
  5. std::set<int> myset;
  6. std::set<int>::iterator it;
  7. std::pair<std::set<int>::iterator,bool> ret;
  8. // set some initial values:
  9. for (int i=1; i<=5; ++i) myset.insert(i*10);    // set: 10 20 30 40 50
  10. ret = myset.insert(20);               // no new element inserted
  11. if (ret.second==false) it=ret.first;  // "it" now points to element 20
  12. myset.insert (it,25);                 // max efficiency inserting
  13. myset.insert (it,24);                 // max efficiency inserting
  14. myset.insert (it,26);                 // no max efficiency inserting
  15. int myints[]= {5,10,15};              // 10 already in set, not inserted
  16. myset.insert (myints,myints+3);
  17. std::cout << "myset contains:";
  18. for (it=myset.begin(); it!=myset.end(); ++it)
  19. std::cout << ' ' << *it;
  20. std::cout << '\n';
  21. return 0;
  22. }
[cpp] view
plain
 copy

  1. #include <iostream>
  2. #include <set>
  3. int main ()
  4. {
  5. std::set<int> myset;
  6. std::set<int>::iterator it;
  7. // insert some values:
  8. for (int i=1; i<10; i++) myset.insert(i*10);  // 10 20 30 40 50 60 70 80 90
  9. it = myset.begin();
  10. ++it;                                         // "it" points now to 20
  11. myset.erase (it);
  12. myset.erase (40);
  13. it = myset.find (60);
  14. myset.erase (it, myset.end());
  15. std::cout << "myset contains:";
  16. for (it=myset.begin(); it!=myset.end(); ++it)
  17. std::cout << ' ' << *it;
  18. std::cout << '\n';
  19. return 0;
  20. }

4)        遍历函数

iterator begin():返回首元素的迭代器指针

iterator end():返回尾元素的迭代器指针

    reverse_iterator rbegin():返回尾元素的逆向迭代器指针

    reverse_iterator rend():返回首元素前一个位置的迭代器指针

[cpp] view
plain
 copy

  1. #include <iostream>
  2. #include <set>
  3. int main ()
  4. {
  5. int myints[] = {75,23,65,42,13};
  6. std::set<int> myset (myints,myints+5);
  7. std::cout << "myset contains:";
  8. for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
  9. std::cout << ' ' << *it;
  10. std::cout << '\n';
  11. return 0;
  12. }

5)        操作函数

const_iterator lower_bound(const Key& key):返回容器中大于等于key的迭代器指针

const_iterator upper_bound(const Key& key):返回容器中大于key的迭代器指针

int count(const Key& key) const:返回容器中元素等于key的元素的个数

    pair<const_iterator,const_iterator> equal_range(const Key& key) const:返回容器中元素值等于key的迭代指针[first, last)

    const_iterator find(const Key& key) const:查找功能,返回元素值等于key的迭代器指针

    void swap(set& s):交换集合元素

    void swap(multiset& s):交换多集合元素

[cpp] view
plain
 copy

  1. #include <iostream>
  2. #include <set>
  3. int main ()
  4. {
  5. std::set<int> myset;
  6. std::set<int>::iterator itlow,itup;
  7. for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
  8. itlow=myset.lower_bound (30);                //       ^
  9. itup=myset.upper_bound (60);                 //                   ^
  10. myset.erase(itlow,itup);                     // 10 20 70 80 90
  11. std::cout << "myset contains:";
  12. for (std::set<int>::iterator it=myset.begin(); it!=myset.end(); ++it)
  13. std::cout << ' ' << *it;
  14. std::cout << '\n';
  15. return 0;
  16. }
[cpp] view
plain
 copy

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <set>
  4. using namespace std;
  5. int main ()
  6. {
  7. set<int> myset;
  8. for (int i=1; i<=5; i++) myset.insert(i*10);   // myset: 10 20 30 40 50
  9. pair<set<int>::const_iterator,set<int>::const_iterator> ret;
  10. ret = myset.equal_range(30);
  11. cout << "the lower bound points to: " << *ret.first << '\n';
  12. cout << "the upper bound points to: " << *ret.second << '\n';
  13. return 0;
  14. }
[cpp] view
plain
 copy

  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <set>
  4. using namespace std;
  5. int main ()
  6. {
  7. int myints[]={12,75,10,32,20,25};
  8. set<int> first (myints,myints+3);     // 10,12,75
  9. set<int> second (myints+3,myints+6);  // 20,25,32
  10. first.swap(second);
  11. cout << "first contains:";
  12. for (set<int>::iterator it=first.begin(); it!=first.end(); ++it)
  13. cout << ' ' << *it;
  14. cout << '\n';
  15. cout << "second contains:";
  16. for (set<int>::iterator it=second.begin(); it!=second.end(); ++it)
  17. cout << ' ' << *it;
  18. cout << '\n';
  19. return 0;
  20. }

set/multiset用法详解的更多相关文章

  1. [STL]set/multiset用法详解[自从VS2010开始,set的iterator类型自动就是const的引用类型]

    集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...

  2. STL:set/multiset用法详解

    集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...

  3. STL之五:set/multiset用法详解

    集合 转载于:http://blog.csdn.net/longshengguoji/article/details/8546286 使用set或multiset之前,必须加入头文件<set&g ...

  4. 7-set用法详解

    C++中set用法详解 转载 http://blog.csdn.net/yas12345678/article/details/52601454 C++ / set 更详细见:http://www.c ...

  5. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  6. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  7. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

  8. mysql中event的用法详解

    一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...

  9. CSS中伪类及伪元素用法详解

    CSS中伪类及伪元素用法详解   伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...

随机推荐

  1. 洛谷 P3332 [ZJOI2013]K大数查询 || bzoj3110

    用树套树就很麻烦,用整体二分就成了裸题.... 错误: 1.尝试线段树套平衡树,码农,而且n*log^3(n)慢慢卡反正我觉得卡不过去 2.线段树pushdown写错...加法tag对于区间和的更新应 ...

  2. HBase简介(很好的梳理资料) 转

    一. 简介 history started by chad walters and jim 2006.11 G release paper on BigTable 2007.2 inital HBas ...

  3. .vue文件在phpstorm中红线解决办法

    主要原因是js版本太低, 1,安装vue.js插件, 2,设置file type,vue.js添加 *.vue, 3,切换js版本为es6,

  4. Echarts生成饼状图、条形图以及线形图 JS封装

    1.在我们开发程序中,经常会用到生成一些报表,比方说饼状图,条形图,折线图等.不多说了,直接上封装好的代码,如下Echarts.js所示 以下代码是封装在Echarts.js文件中 /** * Cre ...

  5. 公众号如何获取已关注用户的unionid的问题

    避免误导,先加一句:首先,得公众号绑定开放平台 这个问题困扰了我一早上,我尝试了很多次获取unionid都失败. 微信的开发文档上有说: 关于特殊场景下的静默授权 1.上面已经提到,对于以snsapi ...

  6. 源代码管理git的使用

    Git ----本地仓库---- 1.新建一个“本地仓库” git init 2.配置仓库 ①告诉git你是谁 git config user.name syl ②告诉git怎么联系你 git con ...

  7. vb,wps,excel 提取括号的数字

    Sub 抽离数字() Dim hang Range("h1").Select Columns("E:F").Select Selection.Clear Ran ...

  8. ubuntu命令行使用ftp客户端

    转载 本篇文章主要介绍在Ubuntu 8.10下如何使用功能强大的FTP客户端软件NcFTP. Ubuntu的源里为我们提供了FTP客户端软件NcFTP,可这款工具对新手来说不是很方便.本文介绍的是一 ...

  9. linux下PPTP Server测试环境搭建

    1.1  服务器软件安装 安裝PPTP  Server 所需的软件: 安装PPTP: sudo apt-get install pptpd PPTP Server的软件安装很简单,只需要安装pptpd ...

  10. Java方法注释模板

    普通方法 /** * ${todo} * @author: SYJP * @version 创建时间:${date} */ 覆盖方法 /** * @Title: ${enclosing_method} ...