zoukankan      html  css  js  c++  java
  • STL之set

    set即集合,主要用来去除重复元素。

    #include<iostream>
    #include<set>
    #include<unordered_set>
    using namespace std;
    
    int main()
    {
        //构造
        set<int> s{ 2,6,8,3,6,9 };
        
        //遍历
        for (set<int>::iterator it = s.begin(); it != s.end(); it++)
            cout << *it;
        cout << endl;
        //输出:23689
        //输出???
        //set元素不能重复且自动从小到大排序
    
        //插入
        s.insert(1);
        s.insert(6);
        s.insert(8);
        s.insert(7);
        for (set<int>::iterator it = s.begin(); it != s.end(); it++)
            cout << *it;
        cout << endl;
        //输出:1236789
    
        //删除指定位置元素
        s.erase(++s.begin());
        for (set<int>::iterator it = s.begin(); it != s.end(); it++)
            cout << *it;
        cout << endl;
        //输出:136789
    
        //删除指定元素
        s.erase(7);
        for (set<int>::iterator it = s.begin(); it != s.end(); it++)
            cout << *it;
        cout << endl;
        //输出:13689
    
        //unordered_set不会自动排序,但速度较快
        unordered_set<int> us{ 2,6,8,3,6,9 };
        for (unordered_set<int>::iterator it = us.begin(); it != us.end(); it++)
            cout << *it;
        cout << endl;
        //输出:26839
    
        //unordered_set无法直接使用sort(),转成vector,见STL之map
        return 0;
    }
  • 相关阅读:
    django 2.0 path
    Django
    ORM
    Django简介
    web框架
    HTTP协议
    web应用
    索引
    pyMysql模块
    视图、触发器、存储过程、函数
  • 原文地址:https://www.cnblogs.com/love-ziji/p/12790160.html
Copyright © 2011-2022 走看看