zoukankan      html  css  js  c++  java
  • STL set容器的一点总结

    整理了一下set常用语句,参看这篇http://www.cnblogs.com/BeyondAnyTime/archive/2012/08/13/2636375.html

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    常用语句

    #include<set>//包含set的头文件

    set<int> p;//定义一个集合容器,不包含重复元素

    multiset<int> p;//定义一个集合容器,包含重复元素

    p.insert(x);//将x元素加入集合中

    p.empty();//判断集合是否为空

    p.clear();//清除集合中所有的元素

    p.count(x)//返回集合中x的个数

    p.size();//返回集合中元素的个数

    p.max_size();//返回容器可能包含的元素最大个数

    p.erase(x);//

    分两种情况==

    1

    p是set型的集合,那么直接删除掉x

    2

    p是multiset型的集合,删掉所有与x相等的元素 但是如果集合里面有很多个x,可是只想删除其中的一个x,传递一个指向想删除掉的这个x的iterator,这时候删除的就是这个x,无返回值 详情参见这一篇----http://book.51cto.com/art/201311/419436.htm

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    set的遍历


    正向遍历
    set<int>::iterator it;//定义迭代器
    it=p.begin();//指向集合的开头
    it=p.end();//指向集合的末尾


    反向遍历
    set<int>::reverse_iterator it;//定义反向迭代器
    it=rbegin();//返回指向集合中最后一个元素的反向迭代器
    it=rend();//返回指向集合中第一个元素的反向迭代器

     1 #include<iostream>
     2 #include<set>
     3 using namespace std;
     4 
     5 set<int>p;
     6 
     7 int main()
     8 {
     9     p.insert(1);
    10     p.insert(2);
    11     p.insert(3);
    12     p.insert(4);
    13     p.insert(5);
    14     
    15     for(set<int>::iterator it=p.begin();it!=p.end();++it)//正向遍历 
    16     cout<<*it<<" ";
    17     
    18     for(set<int>::reverse_iterator it=p.rbegin();it!=p.rend();++it)//反向遍历 
    19     cout<<*it<<" ";    
    20 }
  • 相关阅读:
    rpm离线安装 ZZ https://www.cnblogs.com/Dev0ps/p/11027550.html
    系统记录一下用snmp获得华为/华3 设备cpu使用率的问题
    iperf3使用介绍
    ubuntu16上安装mysql并配置
    etcdserver: mvcc: database space exceeded的处理
    zz kafka的一些细节信息
    ZZ kafka性能问题调优
    日志处理时,遇到多种re表达式轮循匹配的高性能处理方式
    zz 微信小程序image组件中aspectFill和widthfix模式应用详解
    单元格移动时高亮
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4296248.html
Copyright © 2011-2022 走看看