STL set容器常用API

set容器,容器内部将数据自动排序(平衡二叉树),不能插入重复元素。multiset可以插入重复元素。不能修改容器中的值,通过删除值,在插入。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<string>
#include<algorithm>
using namespace std;

struct MyComapre{
    bool operator()(int v1, int v2){
        return v1 > v2;
    }
};

void printMySet(set<int, MyComapre> &s){

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

//1. 
/*
set<T> st;//set默认构造函数:
mulitset<T> mst; //multiset默认构造函数:
set(const set &st);//拷贝构造函数

insert(elem);//在容器中插入元素。
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器。
erase(beg, end);//删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(elem);//删除容器中值为elem的元素。

*/


void test01(){
    
    set<int, MyComapre> s;
    s.insert(4);
    pair<set<int, MyComapre>::iterator, bool> ret = s.insert(2);
    if (ret.second){
        cout << "第一次插入成功!" << endl;
    }
    else{
        cout << "第一次插入失败!" << endl;
    }
    s.insert(3);
    s.insert(9);
    s.insert(6);

    ret = s.insert(2);
    if (ret.second){
        cout << "第一次插入成功!" << endl;
    }
    else{
        cout << "第一次插入失败!" << endl;
    }

    //删除第一个元素的
    s.erase(2);
    s.erase(s.begin());
    //set容器迭代器什么类型?双向迭代器

    printMySet(s);
}

void print(const int &val){
    cout << val << " ";
}

void test02(){
    
    multiset<int> ms;
    ms.insert(7);
    ms.insert(4);
    ms.insert(9);
    ms.insert(2);
    ms.insert(10);
    ms.insert(2);

    for_each(ms.begin(), ms.end(), print);
    cout << endl;
}

//3. set查找操作
/*
find(key);//查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//查找键key的元素个数
lower_bound(keyElem);//返回第一个key>=keyElem元素的迭代器。
upper_bound(keyElem);//返回第一个key>keyElem元素的迭代器。
equal_range(keyElem);//返回容器中key与keyElem相等的上下限的两个迭代器。
*/

void test03(){
    
    set<int> s;
    s.insert(1);
    s.insert(2);
    //s.insert(3);
    s.insert(4);
    s.insert(5);

    set<int>::iterator it = s.find(12);
    if (it == s.end()){
        cout << "查找失败!" << endl;
    }
    else{
        cout << "查找到的值是:" << *it << endl;
    }

    cout << "值为2的元素有:" << s.count(12) << "个!" << endl;

    //lower_bound  upper_bound
    it =  s.lower_bound(2);
    if (it == s.end()){
        cout << "查找失败!" << endl;
    }
    else{
        cout << "查找到的值是:" << *it << endl;
    }

    it = s.upper_bound(2);
    if (it == s.end()){
        cout << "查找失败!" << endl;
    }
    else{
        cout << "查找到的值是:" << *it << endl;
    }
    cout << "------------------------" << endl;

    pair<set<int>::iterator, set<int>::iterator> ret = s.equal_range(2);
    cout << *(ret.first) << endl;
    cout << *(ret.second) << endl;

}

//自定义数据类型
class Person{
public:
    Person(string name,int age){
        this->mName = name;
        this->mAge = age;
    }
public:
    string mName;
    int mAge;
};

struct PersonRule{
    bool operator()(const Person &p1,const Person &p2){
        return p1.mAge > p2.mAge;
    }
};

void test04(){

    set<Person, PersonRule> s;

    s.insert(Person("aaa", 10));
    s.insert(Person("bbb", 70));
    s.insert(Person("ccc", 50));
    s.insert(Person("ddd", 30));
    s.insert(Person("eee", 40));

    for (set<Person, PersonRule>::iterator it = s.begin(); it != s.end();++it){
        cout << "Name:" << it->mName << " Age:" << it->mAge << endl;
    }


    cout << "----------------------" << endl;

    //!PersonRule()(p1, p2) && !PersonRule()(p2, p1)

    set<Person, PersonRule>::iterator it =  s.find(Person("aaa", 20));
    if (it == s.end()){
        cout << "查找失败!" << endl;
    }
    else{
        cout << "Name:" << it->mName << " Age:" << it->mAge << endl;
    }

}

int main(){

    //test01();
    //test02();
    //test03();
    test04();


    system("pause");
    return EXIT_SUCCESS;
}
原文地址:https://www.cnblogs.com/w-x-me/p/6796092.html