天天看點

【C++程式設計語言】C++ 的 Set和Multiset容器 集合容器

作者:煩人的星辰

#挑戰30天在頭條寫日記#

1.Set基本概念

簡介:

  • 所有元素都會在插入時自動被排序

本質:

  • set/multiset屬于關聯式容器 底層結構是用二叉樹實作

set和multiset差別:

  • set不允許容器中有重複的元素
  • multiset允許容器中有重複的元素

2.set構造和指派

功能描述:

建立set容器以及指派

/*
    構造
        set<int>  預設構造函數
        set<const set &st>  拷貝構造函數
    指派
        set &operator=(const set &st)  重載等号操作符
*/
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << endl;
    }
}
void test01() {

    set<int> s1;

    //插入資料  隻有insert方式
    s1.insert(40);
    s1.insert(30);
    s1.insert(20);
    s1.insert(10);

    //周遊容器
    //set容器特點:所有元素插入時候自動被排序
    //set容器不允許插入重複值
    printSet(s1);///輸出:10 20 30 40

    //拷貝構造
    set<int> s2(s1);

    //指派
    set<int> s3 = s2;
}
int main() {
    test01();
    system("pause");
    return 0;
}           

3.set大小和交換

功能描述:

統計set容器大小及交換set容器

/*
    size()   傳回容器中元素的數目
    empty()  判斷容器是否為空
    swap(st)  交換兩個集合容器
*/
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << endl;
    }
}
void test01() {

    set<int> s1;

    //插入資料
    s1.insert(40);
    s1.insert(50);
    s1.insert(20);
    s1.insert(10);

    //列印容器
    printSet(s1);

    //判斷容器是否為空
    if (s1.empty()) {
        cout << "s1為空" << endl;
    }else {
        cout << "s1不為空" << endl;
        cout << "s1的大小為:" << s1.size() << endl;
    }

    //交換
    set<int> s2;
    s2.insert(12);
    s2.insert(52);
    s2.insert(16);
    s2.insert(24);

    s2.swap(s1);
    printSet(s1);
}
int main() {
    test01();
    system("pause");
    return 0;
}           

4.set插入和删除

功能描述:

set容器進行插入資料和删除資料

/*
    insert()   在容器中插入元素
    clear()    清除所有元素
    erase(pos)  删除pos疊代器所指的元素  傳回下一個元素的疊代器
    erase(beg,end)  删除區間(beg,end)的所有元素  傳回下一個元素的疊代器
    erase(elem)  删除容器中值為elem的元素

    疊代器不是随機疊代器
*/
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
void test01() {

    set<int> s1;

    //插入資料
    s1.insert(40);
    s1.insert(50);
    s1.insert(20);
    s1.insert(10);

    //周遊容器
    printSet(s1);

    //删除疊代器所指的内容
    s1.erase(s1.begin());
    printSet(s1);

    //删除重載  删除值為elem的
    s1.erase(50);
    printSet(s1);

    //删除  另類清空
    s1.erase(s1.begin(), s1.end());

    //清空
    s1.clear();
}
int main() {
    test01();
    system("pause");
    return 0;
}           

5.set查找和統計

功能描述:

對set容器進行查找資料以及統計資料

/*
    find(key)   查找key是否存在,若存在,傳回該鍵的元素的疊代器;若不存在,傳回set.end()
    count(key)  統計key的元素個數

*/
void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
void test01() {

    set<int> s1;

    //插入資料
    s1.insert(40);
    s1.insert(50);
    s1.insert(20);
    s1.insert(10);

    //查找
    //疊代器接收資料
    set<int>::iterator pos = s1.find(30);
    if (pos != s1.end()) {
        cout << "找到元素: " << *pos << endl;
    }else {
        cout << "未找到元素: " << endl;
    }

    //統計
    int num = s1.count(50);
    //對于set而言,統計結果要麼是0,要麼是1  因為set容器不能存放重複資料
    cout << num  << endl;

}
int main() {
    test01();
    system("pause");
    return 0;
}           

6.set和multiset差別

差別:

  • set不可插入重複資料,而multiset可以
  • set插入資料的同時會傳回插入結果,表示插入是否成功
  • multiset不會檢測資料,是以可以插入重複資料

7.set容器的排序

set容器預設排序規則為從小到大,可以改變預設排序規則

7.1 set存放内置的資料類型如何改變排序方式

#include "set和multiset.h"
#include<iostream>
#include <set>
using namespace std;

//set容器排序
class MyCompare {
public:
    bool operator()( int v1,  int v2) const {
        return v1 > v2;
    }
};

void printSet(set<int>& s) {
    for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

void test01() {

    set<int> s1;

    //插入資料
    s1.insert(40);
    s1.insert(50);
    s1.insert(20);
    s1.insert(10);

    //内置函數排序從小到大
    printSet(s1);

    //指定排序規則為從大到小
    set<int,MyCompare> s2;

    //插入資料
    s2.insert(40);
    s2.insert(50);
    s2.insert(20);
    s2.insert(10);
    for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
        cout << *it << endl;
    }

}
int main() {
    test01();
    system("pause");
    return 0;
}           

7.2 set存放自定義資料類型如何改變排序方式

class Person {
public:
    string m_Name;
    int m_Age;
    Person(string name,int age) {
        this->m_Name = name;
        this->m_Age = age;
    }
};
class comparePerson {
public:
    bool operator()(const Person& p1, const Person& p2) {
        //按照年齡 降序
        return p1.m_Age > p2.m_Age;
    }
};
void test01() {

    set<Person> s1;

    //建立Person對象
    Person p1("劉備",35);
    Person p2("曹操",45);
    Person p3("孫權",30);
    Person p4("諸葛亮",35);
    Person p5("司馬懿",40);
    Person p6("魯肅",35);

    s1.insert(p1);
    s1.insert(p2);
    s1.insert(p3);
    s1.insert(p4);
    s1.insert(p5);
    s1.insert(p6);

    for (set<Person,comparePerson>::iterator it = s1.begin(); it != s1.end(); it++) {
        cout <<"姓名:" << it->m_Name <<"年齡: " << it->m_Age << endl;
    }
}
int main() {
    test01();
    system("pause");
    return 0;
}           

繼續閱讀