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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM