C++ STL Unordered Multimap – std::unordered_multimap

In this tutorial we will learn about stl unordered multimap container in C++.

Unordered multimap is an associative container. Same asunordered map it stores key and value pair. But unordered map doesn’t allow duplicate values. Here duplicate values are allowed. Since these are unordered containers, there is no order in the way of storing elements. But this allows duplicates so the elements which have same key are grouped together in the same bucket. For duplicate keys another count value is maintained with each key – value pair.

Since unordered multimap useshash table to store key – value pairs, time complexity based on internal hash function used. It performs constant time in average case and in worst case it will take linear time for any operation.

C++ STL Unordered Multimap – std::unordered_multimap

Iterators that can be applicable on unordered multimap:

begin():returns iterator to the beginning.

end():returns iterator to the end of the container.

cbegin():Returns constant iterator to the beginning.

cend():Returns constant iterator to the end.

Unordered multimap is just an extra feature of unordered map. So we need to include same unordered map header function to work with unordered multimap.

i.e #include

Now see some functions applicable on unordered multimap:

i nsert()

There are different types of insertions. In any type we should represent elements in a pair like structure. Since this stores key value pairs.

i.e { “Key” , “value” } format.

Different inserting ways:

  • We can directly initialize key – value pairs placing “=” symbol after declaration.
  • Using “insert” function also we can insert.
  • We can make pair using std :: pair
    and we can insert this pair into unordered multimap.
  • Or we can directly move the pair.
  • If there is another containers which have key – value pairs, by specifying a range we can copy those pairs into to unordered multimap.

erase()

Same as insertion, we can perform deletion also in different ways.

  • Deleting by pointing one of the pair using iterator.
  • Deleting using “key”.

clear(): Clear operation used to remove all key value pairs. By result container becomes empty.

empty():empty() is a Boolean function. It is used to check whether the container empty or not?

Example program to show above functions:

#include 
#include 
#include 
 
using namespace std;
 
int main ()
{
    unordered_multimap  unmmp1;
    unordered_multimap  :: iterator it;
    
    // direct assigning key - value pairs
    unmmp1 = { {"Rohit", 264} , {"Guptil" , 237}};
    
    // inserting using "initializor list" function
    unmmp1.insert ( {{"Sehwag" , 219} , {"Gayle" , 215}} );
    
    // making pair and inserting
    pair  hit2 ("Rohit" , 209);
    unmmp1.insert (hit2);
    
    // moving pair
    unmmp1.insert (make_pair  ("Rohit" , 208));
    
    cout << "Unordered multimap contains: " << endl;
    for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
        cout << "(" <first << " , " <second << ")" << endl;
    }
    
    cout << "Erasing first pair " << endl;
    unmmp1.erase (unmmp1.begin());
    cout << "Erasing record contains key as "Guptil" " << endl;
    unmmp1.erase ("Guptil");
    
    cout << "After performing above erase operations...." << endl;
    for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
        cout << "(" <first << " , " <second << ")" << endl;
    }
    
    cout << "Applying clear() operation... " << endl;
    unmmp1.clear();
    cout << "Checking unordered multi map empty or not " << endl;
    bool chk= unmmp1.empty();
    if (chk == 1) cout << "Unordered multi map is empty " << endl;
    else cout << "Unordered multi map is not empty " << endl;
    
    return 0;
}

Output

Unordered multimap contains:

(Gayle , 215)

(Sehwag , 219)

(Rohit , 208)

(Rohit , 209)

(Rohit , 264)

(Guptil , 237)

Erasing first pair

Erasing record contains key as “Guptil”

After performing above erase operations….

(Sehwag , 219)

(Rohit , 208)

(Rohit , 209)

(Rohit , 264)

Applying clear() operation…

Checking unordered multi map empty or not

Unordered multi map is empty

size():size() operation used to know how many key value pairs present in the unordered multimap.

max_size():max_size() returns maximum size of the container.

find():

  • Find is used to search a particular data pair.
  • We give key as a parameter to this function.
  • If key is present iterator points to it’s position.
  • If given key is not present iterator points to end of the container.

count(): Count used will return how many times a pair present in the container with given key. We give key as a parameter to count function.

Example program to show above functions:

#include 
#include 
#include 
 
using namespace std;
 
int main ()
{
    unordered_multimap  unmmp1;
    unordered_multimap  :: iterator it;
    unmmp1 = { {"Rohit", 264} , {"Guptil" , 237} , {"Rohit", 209} , {"Rohit", 208} };
    
    cout << "Unordered multimap contains: " << endl;
    for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
        cout << "(" <first << " , " <second << ")" << endl;
    }
    
    cout << "Size of the unordered multimap is " ;
    cout << unmmp1.size() << endl ;
    cout << "Maximum Size of the unordered multimap is " ;
    cout << unmmp1.max_size() << endl;
    
    cout << "Finding "Guptil data " " << endl;
    it = unmmp1.find("Guptil");
    if (it!=unmmp1.end())
        cout << "Key found and it's value is " <second << endl;
    else
        cout << "Data not found with given key " << endl;
    
    cout << endl << "Counting how many times Rohit key is present " << endl;
    cout << "Rohit scored 200+ runs " << unmmp1.count("Rohit") << " times " << endl;
    
    return 0;
}

Output

Unordered multimap contains:

(Guptil , 237)

(Rohit , 208)

(Rohit , 209)

(Rohit , 264)

Size of the unordered multimap is 4

Maximum Size of the unordered multimap is 329406144173384850

Finding “Guptil data ”

Key found and it’s value is 237

Counting how many times Rohit key is present

Rohit scored 200+ runs 3 times

swap(): swap() operation applied on two maps. Bu result all key – value pairs of unordered multi map1 are transferred to unordered multi map2 and vice –versa.

Example program to show swap operation:

#include 
#include 
 
using namespace std;
 
int main ()
{
    unordered_multimap  unmmp1, unmmp2;
    unordered_multimap  :: iterator it;
    
    unmmp1 = { {"Rohit", 264} , {"Guptil" , 237} , {"Sehwag" , 219} , {"Rohit", 209} };
    cout << "Unordered multimap :: 1  Before swap: " << endl;
    for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
        cout << "(" <first << " , " <second << ")" << endl;
    }
    
    unmmp2 = { {"Sachin" , 49} , {"Kohli" , 32} , {"Ponting" , 30} , {"JayaSurya" , 28} };
    cout << "Unordered multimap :: 2  Before swap: " << endl;
    for (it= unmmp2.begin(); it!= unmmp2.end(); ++it) {
        cout << "(" <first << " , " <second << ")" << endl;
    }
    
    cout << endl << "Performing swap operation " << endl;
    unmmp1.swap(unmmp2);
    cout << "Unordered multimap :: 1  After swap: " << endl;
    for (it= unmmp1.begin(); it!= unmmp1.end(); ++it) {
        cout << "(" <first << " , " <second << ")" << endl;
    }
    
    unmmp2 = { {"Sachin" , 49} , {"Kohli" , 32} , {"Ponting" , 30} , {"JayaSurya" , 28} };
    cout << "Unordered multimap :: 2  After swap: " << endl;
    for (it= unmmp2.begin(); it!= unmmp2.end(); ++it) {
        cout << "(" <first << " , " <second << ")" << endl;
    }
    return 0;
}

Output

Unordered multimap :: 1 Before swap:

(Sehwag , 219)

(Guptil , 237)

(Rohit , 209)

(Rohit , 264)

Unordered multimap :: 2 Before swap:

(Ponting , 30)

(Kohli , 32)

(JayaSurya , 28)

(Sachin , 49)

Performing swap operation

Unordered multimap :: 1 After swap:

(Ponting , 30)

(Kohli , 32)

(JayaSurya , 28)

(Sachin , 49)

Unordered multimap :: 2 After swap:

(Ponting , 30)

(Kohli , 32)

(JayaSurya , 28)

(Sachin , 49)

稿源:The Crazy Programmer (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » C++ STL Unordered Multimap – std::unordered_multimap

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录