天天看點

C++學習筆記-set容器

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

void myprintf(set<int> &set1);
//集合,元素唯一 不能按照[]方式插入元素 自動排序預設從小到大
//紅黑樹
void myfun()
{
    set<int> set1;
    for (int i = ; i < ; i++)
    {
        int tmp = rand();
        set1.insert(tmp);
    }
    //插入重複元素,隻出現一個,因為是集合
    set1.insert();
    set1.insert();
    set1.insert();
    myprintf(set1);
    cout << endl;
    //删除集合中的元素
    set<int>::iterator it = set1.begin();
    while (set1.size() > )
    {   
        it = set1.begin();

        set1.erase(*it);
        myprintf(set1);
        cout << endl;
    }


}
void myprintf(set<int> &set1)
{
    if (set1.size() == ) cout << "empty" << endl;
    for (set<int>::iterator it = set1.begin(); it != set1.end(); it++)
    {
        cout << *it << " ";
    }
}
//對基本的資料類型set能自動排序
void myfun2()
{
    set<int> set1;
    set<int, less<int>> set2;
    //set<int, greater<int>> set3;//從大到小 不能用 報錯 why???
    for (int i = ; i < ; i++)
    {
        int tmp = rand();
        set1.insert(tmp);
    }
}
// 對複雜的資料類型 Teacher Student

class Student
{
public:
    Student(char *name,int age)
    {
        strcpy_s(this->name, name);
        this->age = age;
    }
public:
    char name[];
    int age;
};
//仿函數
struct FunStudent
{
    bool operator()(const Student &left, const Student &right)
    {
        if (left.age < right.age) return true;//從小到大 按照年齡排序
        else return false;
    }
};
void main93()
{
    Student s1("a1", );
    Student s2("b2", );
    Student s3("c3", );
    Student s4("d4", );
    Student s5("d5", );//如果兩個22歲,能插入成功嗎?of course not

    set<Student,FunStudent> set1;
    set1.insert(s1);
    set1.insert(s2);
    set1.insert(s3);
/*  set1.insert(s4);
    set1.insert(s5);*/ 


//如何知道插入的結果
//  _Pairib insert(const value_type& _Val)
// typedef pair<iterator, bool> _Pairib;
//如何知道set1.insert的傳回值

    pair<set<Student, FunStudent>::iterator, bool> p1 = set1.insert(s4);
    pair<set<Student, FunStudent>::iterator, bool> p2 = set1.insert(s5);
//怎麼用這個傳回值?
    if (p1.second == true)cout << "insert s4 ok" << endl;
    else cout << " insert s4 no" << endl;
    if (p2.second == true)cout << "insert s5 ok" << endl;
    else cout << " insert s5 no" << endl;

    for (set<Student, FunStudent>::iterator it = set1.begin(); it != set1.end(); it++)
    {
        cout << it->age << "\t"<<it->name<<endl;
    }
}

void main95()
{
    set<int> set1;
    for (int i = ; i < ; i++)
    {
        set1.insert(i+);
    }

    for (set<int>::iterator it = set1.begin(); it != set1.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    //搜尋5所在的疊代器的位置
    set<int>::iterator it2 = set1.find();
    cout << *it2 << endl;
    //count 5 的個數
    cout << set1.count() << endl;

    //删除3元素
    set1.erase(); 

    set<int>::iterator it3, it4;
    it3 = set1.lower_bound();//    >=3的疊代器的位置
    cout << *it3 << endl;
    it4 = set1.upper_bound();//    >3的疊代器的位置
    cout << *it4 << endl;

    pair<set<int>::iterator, set<int>::iterator> p1=set1.equal_range();
    set<int>::iterator t3 = p1.first;
    set<int>::iterator t4 = p1.second;
    cout << " [first,second)=[" << *t3 << ", " << *it4 << ")" << endl; 
}
           

繼續閱讀