STL中multiset用法(重复元素集合)

转载自https://blog.csdn.net/sodacoco/article/details/84798621

c++语言中,multiset是库中一个非常有用的类型,它可以看成一个序列,插入一个数,删除一个数都能够在O(logn) 的时间内完成,而且他能时刻保证序列中的数是有序的,而且序列中可以存在重复的数。

应用

#include<string>
#include<iostream>
#include<set>
using namespace std;
int main(){
    
    
    int x;
    scanf("%d",&x);
 //建立一个multiset类型,变量名是h,h序列里面存的是int类型,初始h为空
    multiset<int>h; 
    while(x!=0){
    
    
        h.insert(x);         //将x插入h中
        scanf("%d",&x);
    }    
    while(!h.empty()){
    
           
        auto c=h.begin();c指向h序列中第一个元素的地址,
        printf("%d ",*c);//将地址c存的数据输出
        h.erase(c);   //从h序列中将c指向的元素删除      
    }
    return 0;
}

对于输入数据:32 61 12 2 12 0,该程序的输出是:2 12 12 32 61。

猜你喜欢

转载自blog.csdn.net/weixin_42240667/article/details/103153206