• 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;
    }
  • 相关阅读:
    DAY12 基本余数 运算符2
    DAY11 基本运算符
    DAY10 变量 常量 作用域
    DAY09 JAVA 类型转换
    DAY08 数据类型2
    DAY07 数据类型
    DAY06 JAVA基础语法1注释2标识符
    Beta冲刺集合
    Alpha冲刺集合
    高级软件工程实践总结
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4771061.html
Copyright © 2020-2023  润新知