• C++ STL中的Multimap概述


    传送门 ==>> AutoSAR实战系列300讲「糖果Autosar」总目录

    1 Multimap 概述

    Multimap 类似于map,但多个元素可以具有相同的键。此外,在这种情况下,键值和映射值对不必是唯一的。关于 multimap 需要注意的一件重要事情是 multimap 始终保持所有键的排序顺序。multimap 的这些特性使其在竞争性编程中非常有用。

    与Multimap 相关的一些基本功能:

    begin() – 将迭代器返回到Multimap 中的第一个元素
    end() – 将迭代器返回到Multimap 中最后一个元素之后的理论元素
    size() – 返回Multimap 中的元素数量
    max_size() – 返回Multimap 可以容纳的最大元素数
    empty() – 返回Multimap 是否为空
    pair insert(keyvalue,multimapvalue) – 向Multimap 添加一个新元素

    1 Multimap 代码示例

    用于说明上述功能的 C++ 实现:

    
    // CPP Program to demonstrate the implementation of multimap
    #include 
    #include 
    #include 
    using namespace std;
     
    // Driver Code
    int main()
    {
        multimap<int, int> gquiz1; // empty multimap container
     
        // insert elements in random order
        gquiz1.insert(pair<int, int>(1, 40));
        gquiz1.insert(pair<int, int>(2, 30));
        gquiz1.insert(pair<int, int>(3, 60));
        gquiz1.insert(pair<int, int>(6, 50));
        gquiz1.insert(pair<int, int>(6, 10));
     
        // printing multimap gquiz1
        multimap<int, int>::iterator itr;
        cout << "\nThe multimap gquiz1 is : \n";
        cout << "\tKEY\tELEMENT\n";
        for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
            cout << '\t' << itr->first << '\t' << itr->second
                 << '\n';
        }
        cout << endl;
     
        // adding elements randomly,
        // to check the sorted keys property
        gquiz1.insert(pair<int, int>(4, 50));
        gquiz1.insert(pair<int, int>(5, 10));
     
        // printing multimap gquiz1 again
     
        cout << "\nThe multimap gquiz1 after adding extra "
                "elements is : \n";
        cout << "\tKEY\tELEMENT\n";
        for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
            cout << '\t' << itr->first << '\t' << itr->second
                 << '\n';
        }
        cout << endl;
     
        // assigning the elements from gquiz1 to gquiz2
        multimap<int, int> gquiz2(gquiz1.begin(), gquiz1.end());
     
        // print all elements of the multimap gquiz2
        cout << "\nThe multimap gquiz2 after assign from "
                "gquiz1 is : \n";
        cout << "\tKEY\tELEMENT\n";
        for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
            cout << '\t' << itr->first << '\t' << itr->second
                 << '\n';
        }
        cout << endl;
     
        // remove all elements up to
        // key with value 3 in gquiz2
        cout << "\ngquiz2 after removal of elements less than "
                "key=3 : \n";
        cout << "\tKEY\tELEMENT\n";
        gquiz2.erase(gquiz2.begin(), gquiz2.find(3));
        for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
            cout << '\t' << itr->first << '\t' << itr->second
                 << '\n';
        }
     
        // remove all elements with key = 4
        int num;
        num = gquiz2.erase(4);
        cout << "\ngquiz2.erase(4) : ";
        cout << num << " removed \n";
        cout << "\tKEY\tELEMENT\n";
        for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {
            cout << '\t' << itr->first << '\t' << itr->second
                 << '\n';
        }
     
        cout << endl;
     
        // lower bound and upper bound for multimap gquiz1 key =
        // 5
        cout << "gquiz1.lower_bound(5) : "
             << "\tKEY = ";
        cout << gquiz1.lower_bound(5)->first << '\t';
        cout << "\tELEMENT = " << gquiz1.lower_bound(5)->second
             << endl;
        cout << "gquiz1.upper_bound(5) : "
             << "\tKEY = ";
        cout << gquiz1.upper_bound(5)->first << '\t';
        cout << "\tELEMENT = " << gquiz1.upper_bound(5)->second
             << endl;
     
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    输出
    The multimap gquiz1 is : 
        KEY    ELEMENT
        1    40
        2    30
        3    60
        6    50
        6    10
    
    
    The multimap gquiz1 after adding extra elements is : 
        KEY    ELEMENT
        1    40
        2    30
        3    60
        4    50
        5    10
        6    50
        6    10
    
    
    The multimap gquiz2 after assign from gquiz1 is : 
        KEY    ELEMENT
        1    40
        2    30
        3    60
        4    50
        5    10
        6    50
        6    10
    
    
    gquiz2 after removal of elements less than key=3 : 
        KEY    ELEMENT
        3    60
        4    50
        5    10
        6    50
        6    10
    
    gquiz2.erase(4) : 1 removed 
        KEY    ELEMENT
        3    60
        5    10
        6    50
        6    10
    
    gquiz1.lower_bound(5) :     KEY = 5        ELEMENT = 10
    gquiz1.upper_bound(5) :     KEY = 6        ELEMENT = 50
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    Multimap的功能列表

    FunctionDefinition
    multimap::operator=It is used to assign new contents to the container by replacing the existing contents.
    multimap::crbegin() and multimap::crend()crbegin() returns a constant reverse iterator referring to the last element in the multimap container. crend() returns a constant reverse iterator pointing to the theoretical element before the first element in the multimap.
    multimap::emplace_hint()Insert the key and its element in the multimap container with a given hint.
    multimap clear()Removes all the elements from the multimap.
    multimap empty()Returns whether the multimap is empty.
    multimap maxsize()Returns the maximum number of elements a multimap container can hold.
    multimap value_comp() Returns the object that determines how the elements in the multimap are ordered (‘<‘ by default).
    multimap rendReturns a reverse iterator pointing to the theoretical element preceding to the first element of the multimap container.
    multimap::cbegin() and multimap::cend()cbegin() returns a constant iterator referring to the first element in the multimap container. cend() returns a constant iterator pointing to the theoretical element that follows the last element in the multimap.
    multimap::swap()Swap the contents of one multimap with another multimap of same type and size.
    multimap rbeginReturns an iterator pointing to the last element of the container.
    multimap size()Returns the number of elements in the multimap container.
    multimap::emplace() Inserts the key and its element in the multimap container.
    multimap::begin() and multimap::end()begin() returns an iterator referring to the first element in the multimap container. end() returns an iterator to the theoretical element that follows the last element in the multimap.
    multimap upper_bound()Returns an iterator to the first element that is equivalent to multimapped value with key-value ‘g’ or definitely will go after the element with key-value ‘g’ in the multimap.
    multimap::count()Returns the number of matches to element with key-value ‘g’ in the multimap.
    multimap::erase() Removes the key value from the multimap.
    multimap::find()Returns an iterator to the element with key-value ‘g’ in the multimap if found, else returns the iterator to end.
    multimap equal_range()Returns an iterator of pairs. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k.
    multimap insert()Used to insert elements in the multimap container.
    multimap lower_bound()Returns an iterator to the first element that is equivalent to multimapped value with key-value ‘g’ or definitely will not go before the element with key-value ‘g’ in the multimap.
    multimap key_comp() Returns the object that determines how the elements in the multimap are ordered (‘<‘ by default).
  • 相关阅读:
    redis缓存一致性以及解决方案
    6.DesignForPlacement\PlaceHighlightedSymbols
    网络安全能力成熟度模型介绍
    MATLAB函数
    Github 2024-04-03 C开源项目日报 Top10
    mysql—约束
    ArkID 一账通:企业级开源IDaaS/IAM平台系统
    Android-Handler源码解析-Message
    HTML期末学生大作业:基于html+css+javascript+jquery企业餐厅11页 企业网站制作
    平方根倒数快速算法
  • 原文地址:https://blog.csdn.net/huihuige092/article/details/126299977