首页 > 编程语言 > 详细

C++STL 库中set容器应用

时间:2015-08-30 17:17:09      阅读:256      评论:0      收藏:0      [点我收藏+]
#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;
}

 

C++STL 库中set容器应用

原文:http://www.cnblogs.com/program-ccc/p/4771061.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!