zoukankan      html  css  js  c++  java
  • set容器的用法

    Set是一种关联容器,存储有序且唯一的键值;其大多用法和vector相同就不多加解释,只阐述它的特殊性;我个人认为Set容器的局限性还是比较大的,仅认为Set可以在排序和清除不必要元素时使用,因为Set遍历的时候只可以用迭代器,而且在删除元素时不方便。(删除键值除外);

    1.头文件<set>;

    2.添加元素——a.insret();

    3.清除键值元素——a.erase(m); m表示你要清除的元素值,而不是元素的位置;

    4.元素的遍历方向

      前向遍历

        set<int> a;
        for(set<int> :: iterator oss = a.begin();oss != a.end();oss++)
        {
            cout << *oss << " ";
        }

      后序遍历

    for(set<int> :: reverse_iterator oss = a.rbegin();oss != a.rend();oss++)
    {
         cout << *oss << " ";
    }

    5.元素的逆序排列;(简单的——可添加)

    重载“()”操作符;

    struct Comp
    {
        bool operator()(const int&x ,const int&y)//重写操作符operator();
        {
            if(x!=y)
                return x>y;
            else
                return x>y;
        }
    };
    int main()
    {
        set<int,Comp> a;
        a.insert(1);
        a.insert(2);
        a.insert(3);
        for(set<int,Comp> :: iterator oss = a.begin();oss != a.end();oss++)
        {
            cout << *oss << " ";
        }
        cout << endl;
        return 0;
    }
  • 相关阅读:
    Python基础篇 -- 列表
    Python基础篇 -- 字符串
    Python基础篇 -- if while 语句
    Python基础篇 -- 运算符和编码
    Python 入门基础
    Docker知识收藏
    秒表
    Emac
    Android开发
    shell 小工具
  • 原文地址:https://www.cnblogs.com/7750-13/p/7204437.html
Copyright © 2011-2022 走看看