zoukankan      html  css  js  c++  java
  • C++ STL Set使用

    一、set简介

    1. 参考网址:http://www.cplusplus.com/reference/set/set/

    2. set是STL中一种标准关联容器,其键值就是实值,实值就是键值,不可以有重复,所以我们不能通过set的迭代器来改变set的元素的值。它底层使用平衡的搜索树——红黑树实现,插入删除操作时仅仅需要指针操作节点即可完成,不涉及到内存移动和拷贝,所以效率比较高。set,顾名思义是“集合”的意思,在set中元素都是唯一的,而且默认情况下会对元素自动进行升序排列,支持集合的交(set_intersection),差(set_difference) 并(set_union),对称差(set_symmetric_difference) 等一些集合上的操作,如果需要集合中的元素允许重复那么可以使用multiset。

    3. set的部分基本操作

    set.begin()  //返回set容器的第一个元素
    set.end()    //返回set容器的最后一个元素
    set.clear()  //删除set容器中的所有的元素
    set.empty()  //判断set容器是否为空
    set.insert() //插入一个元素
    set.erase()  //删除一个元素
    set.size()   //返回当前set容器中的元素个数

    二、测试例子

    1. int类型测试set的函数

    #include <iostream>
    #include <set>
    
    using namespace std;
    
    //抽象一个函数,用来输出集合set中的值
    void print(set<int> seta)
    {
        set<int>::iterator itor = seta.begin();
        while(itor != seta.end())
        {
            cout << *itor << " ";
            itor++;
        }
        cout << endl;
    }
      
    int main()
    {
        set<int> seta;
        cout << seta.size() << endl;
        cout << seta.empty() << endl;
          cout << "-------------1--------------" << endl;
     
        //用insert函数初始化set
        for (int i = 0; i < 10; i++){
            seta.insert(i);
        }
      
         /*
         //这里也可以使用数组来赋值进行初始化
         int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
         set<int> seta(a, a+10);
         */
        cout << seta.size() << endl;
        cout << seta.empty() << endl;
          cout << "-------------2--------------" << endl;
      
         print(seta);
          cout << "-------------3--------------" << endl;
     
        //测试find函数
        if (seta.find(0) == seta.end()) { //find?
            cout << "0不存在" << endl;
        } else {
            cout << "0存在" << endl;
        }
    
        if (seta.find(11) != seta.end()){
            cout << "11存在" << endl;
        } else {
            cout << "11不存在" << endl;
        }
         cout << "-------------3--------------" << endl;
    
        //测试erase函数
        seta.erase(9);
        cout << "删除9之后seta的值: " << endl;
        print(seta);
         cout << "-------------4--------------" << endl;
    
        //测试插入函数insert
        int a[5] = {11, 12, 13, 14, 15};
        seta.insert(a, a+5);
        cout << "插入后seta的值: " << endl;
        print(seta);
          cout << "-------------5--------------" << endl; 
    
        return 0;
    }
    
    /*
    //执行结果
    # ./pp
    0
    1
    -------------1--------------
    10
    0
    -------------2--------------
    0 1 2 3 4 5 6 7 8 9 
    -------------3--------------
    0存在
    11不存在
    -------------3--------------
    删除9之后seta的值: 
    0 1 2 3 4 5 6 7 8 
    -------------4--------------
    插入后seta的值: 
    0 1 2 3 4 5 6 7 8 11 12 13 14 15 
    -------------5--------------
    */
  • 相关阅读:
    Linux 文件 目录结构探索
    实践 : Ubuntu 上 Testlink 部署
    Linux 查找安装包所在目录的常用方法
    selenium + python 测试环境搭建 (WINDOWS)
    EntityFramework 学习 一 Entity Framework结构体系
    castle windsor学习-------Container Events 容器的事件
    castle windsor学习----ComponentModel construction contributors
    castle windsor学习----- CastleComponentAttribute 特性注册
    castle windsor学习-----Inline dependencies 依赖
    castle windsor学习-----Registering components by conventions
  • 原文地址:https://www.cnblogs.com/hellokitty2/p/14588931.html
Copyright © 2011-2022 走看看