C++的rbtree,multipleset,set等

/**
g++ -o tool main.cpp
*/
#include <stdio.h>

#include <string>
#include <sstream>
#include <set>
using namespace std;

// 直接在元素里面重载操作符
class Elem1
{
public:
    int a;
    int b;
    Elem1(int _a, int _b){
        a = _a;
        b = _b;
    }
    string desc(){
        stringstream ss;
        ss << "a=" << a << ", b=" << b;
        return ss.str();
    }
    bool operator < (const Elem1& obj) const{
        return a < obj.a;
    }
};
void test_struct_operator(){
    multiset<Elem1> o;
    o.insert(Elem1(20, 21));
    o.insert(Elem1(10, 31));
    o.insert(Elem1(30, 11));
    
    printf("test_struct_operator\n");
    for(multiset<Elem1>::iterator ite = o.begin(); ite != o.end(); ++ite){
        Elem1 obj = *ite;
        printf("element is %s\n", obj.desc().c_str());
    }
    printf("\n\n");
}

// 利用functor重载操作符
class Elem2
{
    friend struct functor;
private:
    int a;
    int b;
public:
    Elem2(int _a, int _b){
        a = _a;
        b = _b;
    }
    string desc(){
        stringstream ss;
        ss << "a=" << a << ", b=" << b;
        return ss.str();
    }
};
struct functor{
    bool operator() (const Elem2& obj0, const Elem2& obj1) const{
        return obj0.b < obj1.b;
    }
};
void test_functor_operator(){
    multiset<Elem2, functor> o;
    o.insert(Elem2(20, 21));
    o.insert(Elem2(10, 31));
    o.insert(Elem2(30, 11));
    
    printf("test_functor_operator\n");
    for(multiset<Elem2, functor>::iterator ite = o.begin(); ite != o.end(); ++ite){
        Elem2 obj = *ite;
        printf("element is %s\n", obj.desc().c_str());
    }
    printf("\n\n");
}

// 比较函数
class Elem3
{
public:
    int a;
    int b;
public:
    Elem3(int _a, int _b){
        a = _a;
        b = _b;
    }
    string desc(){
        stringstream ss;
        ss << "a=" << a << ", b=" << b;
        return ss.str();
    }
};
typedef bool (*FunctionCmp)(const Elem3& obj0, const Elem3& obj1);
bool function_cmp(const Elem3& obj0, const Elem3& obj1){
    return obj0.a < obj1.a;
}
void test_function_operator(){
    multiset<Elem3, FunctionCmp> o(function_cmp);
    o.insert(Elem3(20, 21));
    o.insert(Elem3(10, 31));
    o.insert(Elem3(30, 11));
    
    printf("test_functor_operator\n");
    for(multiset<Elem3, FunctionCmp>::iterator ite = o.begin(); ite != o.end(); ++ite){
        Elem3 obj = *ite;
        printf("element is %s\n", obj.desc().c_str());
    }
    printf("\n\n");
}

// 元素存放是指针
// 利用functor重载操作符
class Elem4
{
    friend struct functor_ptr;
private:
    int a;
    int b;
public:
    Elem4(int _a, int _b){
        a = _a;
        b = _b;
    }
    string desc(){
        stringstream ss;
        ss << "a=" << a << ", b=" << b;
        return ss.str();
    }
};
struct functor_ptr{
    bool operator() (const Elem4* obj0, const Elem4* obj1) const{
        return obj0->b < obj1->b;
    }
};
void test_functor_ptr_operator(){
    multiset<Elem4*, functor_ptr> o;
    o.insert(new Elem4(20, 21));
    o.insert(new Elem4(10, 31));
    o.insert(new Elem4(30, 11));
    
    printf("test_functor_operator\n");
    for(multiset<Elem4*, functor_ptr>::iterator ite = o.begin(); ite != o.end(); ++ite){
        Elem4* obj = *ite;
        printf("element is %s\n", obj->desc().c_str());
    }
    printf("\n\n");
}

int main(int /*argc*/, char** /*argv*/){
    test_struct_operator();
    test_functor_operator();
    test_function_operator();
    test_functor_ptr_operator();
    return 0;
}

你可能感兴趣的:(C++的rbtree,multipleset,set等)