使用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()销毁全部元素,释放内存

  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的元素

  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. }
  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():返回首元素前一个位置的迭代器指针

  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):交换多集合元素

  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. }
  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. }
  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. }

最新文章

  1. CSS的一些简单概念
  2. Linux_记录ping命令的日志包括时间戳
  3. WebBrowser使用详解
  4. JS常用的设计模式(15)—— 职责链模式
  5. Kafka Offset Storage
  6. Unobtrusive JavaScript 是什么?
  7. RocketMQ快速入门
  8. Centos7.0 安装 oracle 11g 以及相关问题解决
  9. jsonp及cors
  10. Spring Resource配置
  11. ES--04
  12. Springboot学习笔记(一)-线程池的简化及使用
  13. Balls(扔鸡蛋问题)
  14. 四则运算 C 语言
  15. es6 学习二 Generator
  16. ASP.NET MVC5+EF6+LayUI实战教程,通用后台管理系统框架(1)
  17. 利用Palette库来取得图片中的主要色彩
  18. IOS 多线程 NSThread
  19. java 路径分隔符File.separator 以及 路径两种写法&quot;/&quot;和&quot;\\&quot;
  20. rpm -e 包名 卸载安装的二进制包

热门文章

  1. Asp.Net Core 入门(三) —— 自定义中间件
  2. vue 数据没有驱动视图?
  3. docker运行时设置redis密码并替换redis默认的dump.rdb
  4. 高德地图api之location定位
  5. [IOS初学]ios 第一篇 storyboard 与viewcontroller的关系 - Zoe_J
  6. 「 HDOJ P3887 」 Counting Offspring
  7. MySQL redo log 与 binlog 的区别
  8. 12. KEY_COLUMN_USAGE
  9. 数据结构实验3:C++实现顺序栈类与链栈类
  10. Java学习之for循环打印菱形练习