zoukankan      html  css  js  c++  java
  • STL-set&&multiset 集合

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <set>
     4 
     5 // 仿函数的原型
     6 //struct greaters
     7 //{
     8 //    bool operator()(const int &left,const int &right) const
     9 //    {
    10 //        return left>right;
    11 //    }
    12 //};
    13 
    14 using namespace std;
    15 
    16 int main()
    17 {
    18     // set容器,元素具有唯一性,但与寻常集合 不同,它是有序的
    19     // 元素必修顺序插入,不能在指定位置插入
    20     // 内部结构为红黑二叉树,加入数据的效率居然比vector还快?反正我信了
    21 
    22     // list与vec的比较
    23     //对于忘list中插入数据,很好理解,分配一个新的结点空间,添加到链表尾部即可
    24     //对于往vectorz中添加数据,需要判断当前控件是否够用,不够用的话需要再次分配稍大的一些空间
    25     // 然后把已有的元素拷贝到新的空间区域,然后释放已有的空间。
    26     // 也许有人觉得这个拷贝操作很费时,觉得没有list添加的速度快,然而,事实并不是这样…..
    27 
    28     // 好了,再回归set
    29     // insert,插入函数,重复失败,返回pair类型
    30     // 复杂的数据类型,需要用仿函数确定元素的顺序
    31 
    32     set<int> set1;
    33     set1.insert(10);
    34     set1.insert(20);
    35     set1.insert(5);
    36 
    37     for(set<int>::iterator it=set1.begin();it!=set1.end();++it)
    38     {
    39         cout<<*it<<' ';
    40     }
    41     cout<<endl;
    42 
    43     set1.erase(set1.begin());
    44 
    45     for(set<int>::iterator it=set1.begin();it!=set1.end();++it)
    46     {
    47         cout<<*it<<' ';
    48     }
    49     cout<<endl;
    50 
    51     // 默认是从小到大的顺序
    52     // set<int,less<int>> set1;
    53     // 从大到小的顺序是
    54 
    55     // set<int,greaters<int>> set2;
    56 
    57     pair<set<int>::iterator,bool> pair1=set1.insert(50);
    58     cout<< *pair1.first <<endl;
    59     if(pair1.second==true)
    60     {
    61         cout<<"YES"<<endl;
    62     }
    63 
    64     pair1=set1.insert(20);
    65 
    66     cout<<pair1.second<<endl;
    67 
    68 
    69     // set的数据查找
    70     // lower_bound
    71     // upper_bound
    72     // equal_bound
    73 
    74     // 查找小于等于10的元素的位置
    75     cout<<*set1.lower_bound(10)<<endl;
    76     // 查找大于20的元素的位置
    77     // 注意,这里没有等于哈
    78     cout<<*set1.upper_bound(20)<<endl;
    79 
    80     for(set<int>::iterator it=set1.begin();it!=set1.end();++it)
    81     {
    82         cout<<*it<<' ';
    83     }
    84     cout<<endl;
    85 
    86     // 查找大于等于20的元素的位置
    87     cout<<* set1.equal_range(20).first<<endl;
    88     cout<<* set1.equal_range(20).second<<endl;
    89 
    90     // multiset ,元素可以重复,其余基本操作相似
    91 
    92      return 0;
    93 }
  • 相关阅读:
    【AS3代码】AS调用JS
    【AS3代码】MP3音乐的播放/暂停/设定音量大小
    【AS3代码】在上下文菜单(右键菜单)中添加自定义项
    【AS3代码】更换鼠标箭头样式,并跟随鼠标!
    【AS3代码】创建动态文本
    【AS3代码】播放FLV视频流的三步骤!
    【AS3代码】Timer计时器用法
    【AS3代码】指定间隔时间运行函数 及 停止运行函数
    【AS3代码】Keyboard键盘操作!
    多线程_传送带我们到底能走多远系列(6)
  • 原文地址:https://www.cnblogs.com/jishuren/p/12242308.html
Copyright © 2011-2022 走看看