集合

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

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

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

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

  2. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  3. STL stack 常见用法详解

    <算法笔记>学习笔记 stack 常见用法详解 stack翻译为栈,是STL中实现的一个后进先出的容器.' 1.stack的定义 //要使用stack,应先添加头文件#include &l ...

  4. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  5. STL queue 常见用法详解

    <算法笔记>学习笔记 queue 常见用法详解 queue翻译为队列,在STL中主要则是实现了一个先进先出的容器. 1. queue 的定义 //要使用queue,应先添加头文件#incl ...

  6. STL map 常见用法详解

    <算法笔记>学习笔记 map 常见用法详解 map翻译为映射,也是常用的STL容器 map可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器) 1. map 的定义 / ...

  7. STL set 常见用法详解

    <算法笔记>学习笔记 set 常见用法详解 set是一个内部自动有序且不含重复元素的容器 1. set 的定义 //单独定义一个set set<typename> name: ...

  8. STL vector常见用法详解

    <算法笔记>中摘取 vector常见用法详解 1. vector的定义 vector<typename> name; //typename可以是任何基本类型,例如int, do ...

  9. STL pair 常见用法详解

    <算法笔记>学习笔记 pair 常见用法详解 //pair是一个很实用的"小玩意",当想要将两个元素绑在一起作为一个合成元素, //又不想因此定义结构体时,使用pair ...

  10. STL string 常见用法详解

    string 常见用法详解 1. string 的定义 //定义string的方式跟基本数据类型相同,只需要在string后跟上变量名即可 string str; //如果要初始化,可以直接给stri ...

随机推荐

  1. WeihanLi.Npoi

    WeihanLi.Npoi Intro Npoi 扩展,适用于.netframework4.5及以上和netstandard2.0, .netframework基于NPOI, .netstandard ...

  2. 含有分类变量(categorical variable)的逻辑回归(logistic regression)中虚拟变量(哑变量,dummy variable)的理解

    版权声明:本文为博主原创文章,博客地址:,欢迎大家相互转载交流. 使用R语言做逻辑回归的时候,当自变量中有分类变量(大于两个)的时候,对于回归模型的结果有一点困惑,搜索相关知识发现不少人也有相同的疑问 ...

  3. 验证码识别之w3cschool字符图片验证码(easy级别)

    起因: 最近在练习解析验证码,看到了这个网站的验证码比较简单,于是就拿来解析一下攒攒经验值,并无任何冒犯之意... 验证码所在网页: https://www.w3cschool.cn/checkmph ...

  4. Unity发布至IOS的流程(踩坑记录)

    这篇文章主要用于记录本人亲身经历过的Unity发布到IOS平台所遇到的所有坑(其实也就是一些自己并不明白的强制设定),以便于后续再有类似需求时少走些弯路. 我的环境: Unity 5.2.2 个人版( ...

  5. malloc_stats---检查内存泄露的神器

    在之前的博客中提到过,valgrind可以用来检测内存泄露,但在使用中,往往会遇到一些问题,给调试工作带来很多不必要的麻烦,我自己遇到的有以下两种: (1)内存泄露误检(系统初始化时,可能有一些需要长 ...

  6. Weblogic 12c 集群部署和session复制

    在上一篇Weblogic12c集群搭建的基础上,这一篇介绍Weblogic12c集群应用的部署和session复制. 1.启动服务 首先在weblogic12c控制台,启动受托管服务server1.s ...

  7. Gazebo機器人仿真學習探索筆記(六)工具和实用程序

    Gazebo附带了许多工具和实用程序. 这些教程说明了这些可用的工具,以及如何使用它们. 主要有: 1 记录和播放 2 日志过滤 3 应用力/扭矩 4 HDF5数据集 官网介绍通俗具体,非常容易,请参 ...

  8. 详解EBS接口开发之库存事务处理-物料批次导入

    库存事务处理-物料批次导入 --系统批次表 SELECT * FROM MTL_LOT_NUMBERS T; --API创建批次 inv_lot_api_pub.create_inv_lot(x_re ...

  9. Linux & Windows 计时函数

    直接上代码: #if defined(_WIN32) && defined(_MSC_VER) #include <windows.h> double abtic() { ...

  10. React Native之ListView实现九宫格效果

    概述 在安卓原生开发中,ListView是很常用的一个列表控件,那么React Native(RN)如何实现该功能呢?我们来看一下ListView的源码 ListView是基于ScrollView扩展 ...