码迷,mamicode.com
首页 > 编程语言 > 详细

C++STL 库中set容器应用

时间:2015-08-30 17:18:22      阅读:119      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!