stl学习笔记2

set 和multiset 以后用来做排序很方便啊。 这个就是把元素以排序的状态存放于内部的tree结构。set 不允许元素重复,multiset允许元素重复。写个简单的代码

// stl/ set and multiset  // cpp
//
#include  < iostream >
#include 
< set >
using   namespace  std;

int  main()
{
    typedef 
set<int> IntSet;
    IntSet coll;

    
// 默认从小到到大排列,反方向要像下面用
    typedef set<int,greater<int> > IntSet2;    //两个 > > 要隔开,不然可能出错
    IntSet2 coll2;

    typedef multiset
<int> IntMultiSet;
    IntMultiSet coll3;

    typedef multiset
<int,greater<int> > IntMultiSet2;
    IntMultiSet2 coll4;

    
for(int i =0; i<20;++i) 
    
{
//        srand();
        int num = rand()%50;
        coll.insert(num); 
// 随机插入数字
        coll2.insert(num);
        coll3.insert(num);
        coll4.insert(num);
    }

    IntSet::const_iterator pos;
    
for(pos = coll.begin();pos!=coll.end();++pos)
    
{
        cout 
<<*pos<<' ';
    }

    cout 
<<endl;
    IntSet2::const_iterator pos2;
    
for(pos2 = coll2.begin(); pos2 !=coll2.end();++pos2)
    
{
        cout 
<<*pos2 <<' ';
    }

    cout 
<<endl;
    IntMultiSet::const_iterator pos3;
    
for(pos3 = coll3.begin(); pos3 !=coll3.end(); ++pos3)
    
{
        cout 
<<*pos3 <<' ';
    }

    cout 
<<endl;
    IntMultiSet2::const_iterator pos4;
    
for(pos4 = coll4.begin(); pos4 !=coll4.end(); ++pos4)
    
{
        cout 
<<*pos4<<' ';
    }

    cout 
<<endl;
    
return 1;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值