STL - 容器 - MultiSet

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: MultiSet根据特定排序准则,自动将元素排序。MultiSet允许元素重复。一些常规操作:MultiSetTest.cpp #include #include #include #include #include #include "MultiSetTest.

MultiSet根据特定排序准则,自动将元素排序。
MultiSet允许元素重复。
一些常规操作:
MultiSetTest.cpp

#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <functional>
#include "MultiSetTest.h"

using namespace std;

void MultiSetTest::operationDemo()
{
    // type of the collection:
    // - duplicates allowed
    // - elements are integral values
    // - descending order
    multiset<int, greater<int>> coll1;

    // insert elements in random order using different member functions
    coll1.insert({ 4, 3, 5, 1, 6, 2 });
    coll1.insert(5);

    // print all elements
    for (int elem : coll1) {
        cout << elem << ' ';
    }
    cout << endl;

    // insert 4 again and process return value
    auto ipos = coll1.insert(4);
    cout << "4 inserted as element "
        << distance(coll1.begin(), ipos) + 1 << endl;

    // assign elements to another multiset with ascending order
    multiset<int> coll2(coll1.cbegin(), coll1.cend());

    // print all elements of the copy using stream iterators
    copy(coll2.cbegin(), coll2.cend(),
        ostream_iterator<int>(cout, " "));
    cout << endl;

    // remove all elements up to element with value 3
    coll2.erase(coll2.begin(), coll2.find(3));

    // remove all elements with value 5
    int num;
    num = coll2.erase(5);
    cout << num << " element(s) removed" << endl;

    // print all elements
    copy(coll2.cbegin(), coll2.cend(),
        ostream_iterator<int>(cout, " "));
    cout << endl;
}

void MultiSetTest::run()
{
    printStart("operationDemo()");
    operationDemo();
    printEnd("operationDemo()");
}

运行结果:

---------------- operationDemo(): Run Start ----------------
6 5 5 4 3 2 1
4 inserted as element 5
1 2 3 4 4 5 5 6
2 element(s) removed
3 4 4 6
---------------- operationDemo(): Run End ----------------

 

目录
相关文章
|
6天前
|
存储 设计模式 算法
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
【C++/STL】stack和queue(容器适配器、优先队列、双端队列)
13 1
|
13天前
|
存储 算法 C++
详解C++中的STL(标准模板库)容器
【4月更文挑战第30天】C++ STL容器包括序列容器(如`vector`、`list`、`deque`、`forward_list`、`array`和`string`)、关联容器(如`set`、`multiset`、`map`和`multimap`)和容器适配器(如`stack`、`queue`和`priority_queue`)。它们为动态数组、链表、栈、队列、集合和映射等数据结构提供了高效实现。选择合适的容器类型可优化性能,满足不同编程需求。
|
15天前
|
C++ 容器
STL—map容器
STL—map容器
|
19天前
|
存储 算法 程序员
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
C++从入门到精通:2.2.1标准库与STL容器算法深度解析
|
26天前
|
C++ 容器
约瑟夫经典问题C++,STL容器queue解法
约瑟夫经典问题C++,STL容器queue解法
14 0
|
1月前
|
存储 C语言 C++
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
C++中STL常用容器(vector、deque、list、map、set)一文带你了解
|
1月前
|
存储 算法 C++
C++容器STL相关面试问题
C++容器STL相关面试问题
|
2月前
|
存储 C++ 容器
C++之STL顺序容器
C++之STL顺序容器
|
2月前
|
存储 算法 数据处理
【C++ STL容器set 】set 容器的全方位解析
【C++ STL容器set 】set 容器的全方位解析
124 0
|
2月前
|
安全 算法 编译器
【C++ 泛型编程 进阶篇】深入探索 C++ STL 容器的嵌套类型:识别、运用与最佳实践
【C++ 泛型编程 进阶篇】深入探索 C++ STL 容器的嵌套类型:识别、运用与最佳实践
98 7