C++STL 库中set容器应用

#include<iostream>
#include<cstdio>
#include<set>
using namespace std;
set<int> a;
int main()
{
    //插入元素 
    a.insert(1);
    a.insert(3);
    a.insert(5);
    //用迭代器遍历容器; 
    set<int>::iterator it;
    for(it=a.begin(); it!=a.end(); it++)
    {
        cout<<*it<<endl;
    }
    //find(key_value) 若查找成功返回迭代器位置,否则返回容器最后一个元素的后一个位置 
    if(a.find(1)!=a.end())
    {
        cout<<"find success"<<endl;
    }
    else
    {
        cout<<"losing finding"<<endl;
    }
    //count(key_value) 若查找成功返回true, 否则返回false  
    if(a.count(1)==true)
    {
        cout<<"find success"<<endl;
    }
    else
    {
        cout<<"losing finding"<<endl;
    }
    //size()返回容器中元素个数 
    cout<<"set中的元素个数是"<<a.size()<<endl;
    //clera()将容器清空 
    a.clear();
    //empty()判断容器是否为空 
    if(a.empty())
    {
        cout<<"set is empty"<<endl;
    }
    else
    {
        cout<<"set is not empty"<<endl;
    }
    
    return 0;
}

 

posted on 2015-08-30 16:04  vCoders  阅读(251)  评论(0编辑  收藏  举报

导航