multiset学习之创建,初始化,赋值操作operator=, empty,size

本篇开始学习multiset,与set相比,multiset可以有多个相关的关键字,其它也set的功能一样,下面我们先来学习它的创建,初始化,赋值操作operator=, empty,size等相关操作。

代码示例:

#include 
#include 
#include 
#include 
#include 

using namespace std;


bool fncomp_greater (int lhs, int rhs) {return lhs > rhs;}

bool fncomp_less (int lhs, int rhs) {return lhs < rhs;}

struct classcomp_less {
    bool operator() (const int& lhs, const int& rhs) const
    {return lhs < rhs;}
};

struct classcomp_greater {
    bool operator() (const int& lhs, const int& rhs) const
    {return lhs > rhs;}
};

struct Point { int x, y; };
struct PointCmp_greater {
    bool operator()(const Point& lhs, const Point& rhs) const {
        return std::hypot(lhs.x, lhs.y) > std::hypot(rhs.x, rhs.y);//hypot计算三解形的斜边长
    }
};
struct PointCmp_less {
    bool operator()(const Point& lhs, const Point& rhs) const {
        return std::hypot(lhs.x, lhs.y) < std::hypot(rhs.x, rhs.y);
    }
};

void multisetInit()
{
    //1.创建multiset
    string strVal[] = {"first", "second", "three", "fifth"};
    multiset set1(strVal, strVal + 4);
    cout << "set1.size = " << set1.size() << " set1.empty = " << set1.empty() << " set1.max_size = " << set1.max_size() << endl;

    cout << "set1.size = " << set1.size() << " set1.empty = " << set1.empty() << " set1.max_size = " << set1.max_size() << endl;
    cout << "set1的值为:  ";
    multiset::iterator iter1;
    for(iter1 = set1.begin(); iter1 != set1.end(); iter1++)
    {
        cout << *iter1 << "\t";
    }
    cout << endl;
    //2.列表初始化
    multiset set2 = {1, 2, 3, 4};
    cout << "set2.size = " << set2.size() << " set2.empty = " << set2.empty() << " set2.max_size = " << set2.max_size() << endl;
    cout << "set2的值为:  ";
    for(auto &val: set2)
    {
        cout << val << "\t";
    }
    cout << endl;
    //3.赋值初始化
    multiset set3 = set2;
    cout << "set3的值为:  ";
    for(auto &val: set3)
    {
        cout << val << "\t";
    }
    cout << endl;
    //4.拷贝初始化
    multiset set4(set1);
    cout << "set4.size = " << set4.size() << " set4.empty = " << set4.empty() << " set4.max_size = " << set4.max_size() << endl;
    cout << "set4的值为:  ";
    for(auto &val: set4)
    {
        cout << val << "\t";
    }
    cout << endl;
    //5.范围初始化
    multiset set5(set2.begin(), set2.end());
    cout << "set5.size = " << set5.size() << " set5.empty = " << set5.empty() << " set5.max_size = " << set5.max_size() << endl;
    cout << "set5的值为:  ";
    for(auto &val: set5)
    {
        cout << val << "\t";
    }
    cout << endl;
    //6.移动构造函数
    multiset set6(std::move(set1));
    cout << "set6.size = " << set6.size() << " set6.empty = " << set6.empty() << " set6.max_size = " << set6.max_size() << endl;
    auto iter6 = set6.begin();
    cout << "set6的值为:  ";
    while (iter6 != set6.end())
    {
        cout << *iter6 << "\t";
        iter6++;
    }
    cout << endl;
    cout << "-----------------------" << endl;

    // class as Compare
    std::multiset set7 = {1, 7, 3, 4, 3};
    auto iter7 = set7.begin();
    //默认是升序输出的
    cout << "默认是升序输出的  set7的值为:  ";
    while (iter7 != set7.end())
    {
        cout << *iter7 << "\t";
        iter7++;
    }
    cout << endl;

    std::multiset set8 = {1, 7, 3, 4, 3};
    auto iter8 = set8.begin();
    cout << "升序输出        set8的值为:  ";//升序输出
    while (iter8 != set8.end())
    {
        cout << *iter8 << "\t";
        iter8++;
    }
    cout << endl;

    std::multiset set9 = {1, 7, 3, 4, 3};
    auto iter9 = set9.begin();
    cout << "降序输出        set9的值为:  ";//降序输出
    while (iter9 != set9.end())
    {
        cout << *iter9 << "\t";
        iter9++;
    }
    cout << endl;
    //也可以调用系统自带的比较函数,例如greater,less, 要包含头文件functional
    std::multiset> set10 = {1, 7, 3, 4, 3};
    auto iter10 = set10.begin();
    cout << "降序输出 set10的值为:  ";//降序输出
    while (iter10 != set10.end())
    {
        cout << *iter10 << "\t";
        iter10++;
    }
    cout << endl;
    std::multiset> set11 = {1, 7, 3, 4, 3};
    auto iter11 = set11.begin();
    cout << "升序输出 set11的值为:  ";//降序输出
    while (iter11 != set11.end())
    {
        cout << *iter11 << "\t";
        iter11++;
    }
    cout << endl;

    bool(*fn_pt)(int,int) = fncomp_less;
    std::multiset set12 (fn_pt);  // function pointer as Compare
    set12.insert(1);
    set12.insert(7);
    set12.insert(4);
    set12.insert(3);
    auto iter12 = set12.begin();
    cout << "升序输出 set12的值为:  ";//降序输出
    while (iter12 != set12.end())
    {
        cout << *iter12 << "\t";
        iter12++;
    }
    cout << endl;
    bool(*fn_pt_greater)(int,int) = fncomp_greater;
    std::multiset set13 (fn_pt_greater);
    set13.insert(1);
    set13.insert(7);
    set13.insert(4);
    set13.insert(3);
    auto iter13 = set13.begin();
    cout << "升序输出 set13的值为:  ";//升序输出
    while (iter13 != set13.end())
    {
        cout << *iter13 << "\t";
        iter13++;
    }
    cout << endl;
    //自定义类型比较
    std::multiset set14 = {{1, 2}, {7, 8}, {3, 4}, {3, 5}};
    set14.insert({8, 7}); // 这会失败,因为 8,7 的斜边长度等于 7,8
    cout << "降序输出 set14的值为:    ";
    for(auto& p: set14)
        cout << '(' << p.x << ',' << p.y << ") ";
    cout << endl;
    std::multiset set15 = {{1, 2}, {7, 8}, {3, 4}, {3, 5}};
    set15.insert({8, 7});
    cout << "升序输出 set15的值为:    ";
    for(auto& p: set15)
        cout << '(' << p.x << ',' << p.y << ") ";

    cout << endl;
}

int main()
{
    multisetInit();

    cout << "Hello, world!" << endl;
    return 0;
}

运行结果:

multiset学习之创建,初始化,赋值操作operator=, empty,size_第1张图片

 

参考:

https://zh.cppreference.com/w/cpp/container/multiset

你可能感兴趣的:(STL,multiset)