初学STL中set用法

转载:http://blog.csdn.net/chaoyueziji123/article/details/38422211

set使用方法:

begin()    ,返回set容器的第一个迭代器
end()      ,返回set容器的最后一个迭代器
clear()    ,删除set容器中的所有的元素
empty()    ,判断set容器是否为空
max_size()   ,返回set容器可能包含的元素最大个数
size()      ,返回当前set容器中的元素个数
rbegin     ,返回的值和end()相同
rend()     ,返回的值和rbegin()相同

#include <iostream>  
#include <set>  

using namespace std;  

int main()  
{  
    set<int> s;  
    s.insert(1);  
    s.insert(2);  
    s.insert(3);  
    s.insert(1);  
    cout<<"set 的 size 值为 :"<<s.size()<<endl;  
    cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl;  
    cout<<"set 中的第一个元素是 :"<<*s.begin()<<endl;  
    cout<<"set 中的最后一个元素是:"<<*s.end()<<endl;  
    s.clear();  
    if(s.empty())  
    {  
        cout<<"set 为空 !!!"<<endl;  
    }  
    cout<<"set 的 size 值为 :"<<s.size()<<endl;  
    cout<<"set 的 maxsize的值为 :"<<s.max_size()<<endl;  
    return 0;  
}  

erase(iterator) ,删除定位器iterator指向的值
erase(first,second),删除定位器first和second之间的值
erase(key_value),删除键值key_value的值

#include <iostream>  
#include <set>  

using namespace std;  

int main()  
{  
    set<int> s;  
    set<int>::const_iterator iter;  
    set<int>::iterator first;  
    set<int>::iterator second;  
    for(int i = 1 ; i <= 10 ; ++i)  
    {  
        s.insert(i);  
    }  
    //第一种删除  
    s.erase(s.begin());  
    //第二种删除  
    first = s.begin();  
    second = s.begin();  
    second++;  
    second++;  
    s.erase(first,second);  
    //第三种删除  
    s.erase(8);  
    cout<<"删除后 set 中元素是 :";  
    for(iter = s.begin() ; iter != s.end() ; ++iter)  
    {  
        cout<<*iter<<" ";  
    }  
    cout<<endl;  
    return 0;  
}  

find() ,返回给定值值得定位器,如果没找到则返回end()。

#include <iostream>  
#include <set>  

using namespace std;  

int main()  
{  
    int a[] = {1,2,3};  
    set<int> s(a,a+3);  
    set<int>::iterator iter;  
    if((iter = s.find(2)) != s.end())  
    {  
        cout<<*iter<<endl;  
    }  
    return 0;  
}  

insert(key_value); 将key_value插入到set中 ,返回值是pair

#include <iostream>  
#include <set>  

using namespace std;  

int main()  
{  
    int a[] = {1,2,3};  
    set<int> s;  
    set<int>::iterator iter;  
    s.insert(a,a+3);  
    for(iter = s.begin() ; iter != s.end() ; ++iter)  
    {  
        cout<<*iter<<" ";  
    }  
    cout<<endl;  
    pair<set<int>::iterator,bool> pr;  
    pr = s.insert(5);  
    if(pr.second)  
    {  
        cout<<*pr.first<<endl;  
    }  
    return 0;  
}  

lower_bound(key_value) ,返回第一个大于等于key_value的定位器
upper_bound(key_value),返回最后一个大于等于key_value的定位器

#include <iostream>  
#include <set>  

using namespace std;  

int main()  
{  
    set<int> s;  
    s.insert(1);  
    s.insert(3);  
    s.insert(4);  
    cout<<*s.lower_bound(2)<<endl;  
    cout<<*s.lower_bound(3)<<endl;  
    cout<<*s.upper_bound(3)<<endl;  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值