zoukankan      html  css  js  c++  java
  • C++ STL 之 set 和 pair

    set/multiset 的特性是所有元素会根据元素的值自动进行排序。set 是以 RB-tree(红黑树,平衡二叉树的一种)为底层机制,其查找效率非常好。set 容器中不允许重复元
    素,multiset 允许重复元素。
    我们可以通过 set 的迭代器改变元素的值吗?
    答: 不行,因为 set 集合是根据元素值进行排序,关系到 set 的排序规则,如果任意改变 set 的元素值,会严重破坏 set 组织。
    #include <iostream>
    #include <set>
    #include <list>
    #include <string>
    using namespace std;
    
    void PrintSet(set<int>& s)
    {
        for (set<int>::iterator it = s.begin(); it != s.end(); it++)
        {
            cout << *it << " ";
        }
        cout << endl;
    }
    
    class mycompare
    {
    public:
        bool operator()(int v1, int v2)
        {
            return v1 > v2;
        }
    };
    
    // set初始化
    // set<T> st;//set 默认构造函数:
    // mulitset<T> mst; //multiset 默认构造函数:
    // set(const set &st);//拷贝构造函数
    void test01()
    {
        set<int> s1; // 自动进行排序, 默认从小到大
        s1.insert(7);
        s1.insert(2);
        s1.insert(4);
        s1.insert(5);
        s1.insert(1);
        PrintSet(s1);
        // 赋值操作
        // set& operator=(const set &st);//重载等号操作符
        // swap(st);//交换两个集合容器
        set<int> s2;
        s2 = s1;
        PrintSet(s2);
        // 删除操作
        // insert(elem);//在容器中插入元素。
        // clear();//清除所有元素
        // erase(pos);//删除 pos 迭代器所指的元素,返回下一个元素的迭代器。
        // erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
        // erase(elem);//删除容器中值为 elem 的元素。
        s1.erase(s1.begin());
        s1.erase(7);
        PrintSet(s1);
        cout << "----------------------" << endl;
    }
    
    // set查找
    // find(key);//查找键 key 是否存在,若存在,返回该键的元素的迭代器;若不存在,返回 map.end();
    // lower_bound(keyElem);//返回第一个 key>=keyElem 元素的迭代器。
    // upper_bound(keyElem);//返回第一个 key>keyElem 元素的迭代器。
    // equal_range(keyElem);//返回容器中 key 与 keyElem 相等的上下限的两个迭代器。
    void test02()
    {
        set<int> s1;
        s1.insert(7);
        s1.insert(2);
        s1.insert(4);
        s1.insert(5);
        s1.insert(1);
        set<int>::iterator ret = s1.find(14);
        if (ret == s1.end())
        {
            cout << "没有找到!" << endl;
        }
        else
        {
            cout << "ret: " << *ret << endl;
        }
        // 找第一个大于key的值
        ret = s1.upper_bound(2);
        if (ret == s1.end())
        {
            cout << "没有找到!" << endl;
        }
        else
        {
            cout << "ret: " << *ret << endl;
        }
        // equal_range 返回Lower_bound 和 upper_bound值
        pair<set<int>::iterator, set<int>::iterator> myret = s1.equal_range(2);
        if (myret.first == s1.end())
        {
            cout << "没有找到!" << endl;
        }
        else
        {
            cout << "myret: " << *(myret.first) << endl;
         }
        if (myret.second == s1.end())
        {
            cout << "没有找到!" << endl;
        }
        else
        {
            cout << "myret: " << *(myret.second) << endl;
        }
        cout << "----------------" << endl;
    }
    
    class Person
    {
    public:
        Person(int age, int id) :id(id), age(age){}
    public:
        int id;
        int age;
    };
    
    class mycompare2
    {
    public:
        bool operator()(Person p1, Person p2)
        {
            if (p1.id == p2.id)
            {
                return p1.age > p2.age;
            }
            else
            {
                p1.id > p2.id;
            }
        }
    };
    
    void test03()
    {
        set<Person, mycompare2> sp;
        Person p1(10, 20), p2(20, 20), p3(50, 60);
        sp.insert(p1);
        sp.insert(p2);
        sp.insert(p3);
        Person p4(10, 30);
        for (set<Person, mycompare2>::iterator it = sp.begin(); it != sp.end(); it++)
        {
            cout << (*it).age << "  " << (*it).id << endl;
        }
        set<Person, mycompare2>::iterator ret = sp.find(p4);
        if (ret == sp.end())
        {
            cout << "没有找到!" << endl;
        }
        else
        {
            cout << "找到:" << (*ret).id << " " << (*ret).age << endl;
        }
    }
    
    // 对组
    
    void test04()
    {
        // 构造方法
        pair<int, int> pair1(10, 20);
        cout << pair1.first << " " << pair1.second << endl;
        pair<int, string> pair2 = make_pair(10, "aaaaa");
        cout << pair2.first << " " << pair2.second << endl;
        pair<int, string> pair3 = pair2;
    }
    
    int main()
    {
        test01();
        test02();
        test03();
        test04();
        getchar();
        return 0;
    }
  • 相关阅读:
    convert image to base64 and post to RESTful wcf
    在android webview实现截屏的手动tounchmove裁剪图片
    How to use jquery ajax and android request security RESTful WCF
    using swfUpload in asp.net mvc
    using HttpClient and sending json data to RESTful server in adroind
    ODP.NET数据访问
    android image watermark
    解决国内不能访问github的问题
    idapro权威指南第二版阅读笔记第九章 交叉引用和绘图功能
    idapro权威指南第二版阅读笔记第二章 逆向和反汇编工具
  • 原文地址:https://www.cnblogs.com/duxie/p/10923902.html
Copyright © 2011-2022 走看看