容器Set的简单用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhao3132453/article/details/82889771
#include <iostream>
#include <set>
 
using namespace std;
 
    
int main()
{
    set<int> s; //声明
    s.insert(1);//插入元素
    s.insert(3);
    s.insert(2);
    s.insert(2);//自动去除重复元素
    
    //此时set中元素为1,2,3 自动升序排列
    
    //遍历
    for(set<int>::iterator it=s.begin();it!=s.end();it++)
    {
         cout<<*it<<endl;   
    }   
    
    //第一个元素
    cout<<*s.begin()<<endl;
    
    //最后一个元素
    cout<<*s.rbegin()<<endl;
    cout<<*--s.end()<<endl;
    
    cout<<*s.end()<<endl; //后两种有的编译器可能会报错
    cout<<*s.end()--<<endl;
    
    //删除一个元素
    s.erase(1);
    
    //set的最大容量
    cout<<s.max_size()<<endl;
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhao3132453/article/details/82889771