📜  C++ STL 中的 Multimap 与 Map 示例

📅  最后修改于: 2022-05-13 01:54:23.307000             🧑  作者: Mango

C++ STL 中的 Multimap 与 Map 示例

C++ STL 中的映射

Map 以排序的方式存储唯一的键值对。每个键都与一个值唯一关联,该值可能是唯一的,也可能不是唯一的。可以从映射中插入或删除键,但不能修改。可以更改分配给键的值。这是使用键快速访问值的好方法,并且在 O(1) 时间内完成。

C++
#include 
#include 
#include 
  
using namespace std;
  
int main()
{
  
    // empty map container
    map gquiz1;
  
    // insert elements in random order
    gquiz1.insert(pair(1, 40));
    gquiz1.insert(pair(2, 30));
    gquiz1.insert(pair(3, 60));
    gquiz1.insert(pair(4, 20));
    gquiz1.insert(pair(5, 50));
    gquiz1.insert(pair(6, 50));
    gquiz1.insert(pair(7, 10));
  
    // printing map gquiz1
    map::iterator itr;
    cout << "\nThe map 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;
  
    // assigning the elements from gquiz1 to gquiz2
    map gquiz2(gquiz1.begin(), gquiz1.end());
  
    // print all elements of the map gquiz2
    cout << "\nThe map 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
    // element with key=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 map 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;
}


C++
#include 
#include 
#include 
  
using namespace std;
  
int main()
{
    // empty multimap container
    multimap gquiz1;
  
    // insert elements in random order
    gquiz1.insert(pair(1, 40));
    gquiz1.insert(pair(2, 30));
    gquiz1.insert(pair(3, 60));
    gquiz1.insert(pair(6, 50));
    gquiz1.insert(pair(6, 10));
  
    // printing multimap gquiz1
    multimap::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(4, 50));
    gquiz1.insert(pair(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 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;
}


输出

C++ STL 中的多重映射 

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

C++

#include 
#include 
#include 
  
using namespace std;
  
int main()
{
    // empty multimap container
    multimap gquiz1;
  
    // insert elements in random order
    gquiz1.insert(pair(1, 40));
    gquiz1.insert(pair(2, 30));
    gquiz1.insert(pair(3, 60));
    gquiz1.insert(pair(6, 50));
    gquiz1.insert(pair(6, 10));
  
    // printing multimap gquiz1
    multimap::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(4, 50));
    gquiz1.insert(pair(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 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;
}
输出

C++ STL中Map和Multimap的区别

S No. MapMultimap
1It stores unique key-value pair where each key is unique.It can store duplicate key-value pair where keys may not be unique.
2Using count() function on a map can only return two values which is either 0 or 1.Using count() function on a multimap can return any non-negative integer.
3Accessing Value of any key is easy and directly accessible.Accessing value of any key is not easy and is not directly accessible.
4Deleting in a map using key will delete only one key-value pair.Deleting in a multimap using key will delete all the key-value pair having same key.
5Map can be used when a simple look up table having unique key-value pairs is required for quickly accessing to the value using the key.Multimap can be used when grouping of values together using the keys are required.