Skip to main content

C++ STL Multimap Container – std::multimap

In this tutorial you will learn about stl multimap i.e., std::multimap and all functions applicable on it with some example code.

In previous articles we already learned about std::map container. It is an associative container which give priority to key values. But problem with map is it won’t allow duplicate values. But multimap allows duplicate values. Here in multimap both key and value pair becomes unique. And we already know that one of the great property of map and multimap is always elements automatically inserted into sorted order on fly. Keys can’t be modified once inserted. Only possible way is we have to delete and update with a new value.

Implementation of multimaps follows a binary search tree type of implementation.

C++ STL Multimap Container – std::multimap

To work with multimap we need to include map header file.

#include <map>

Iterators that can be applicable on multimap:

begin(): returns iterator to the beginning.

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

rbegin(): returns reverse iterator to reverse beginning.

rend(): returns reverse iterator to reverse end.

cbegin(): Returns constant iterator to the beginning.

cend(): Returns constant iterator to the end.

Declaring a multimap:

multimap < datatype, datatype > multiMapName;

This is for key and value pair.

Now see some basic operations on multimap:

Insert operation: Insert operations effects size of multimap.

multiMapName.insert (pair (datatype, datatype ) ( keyt, value ))

Another type of insertion is, we can directly assign one multimap elements to other multimap elements directly by giving range using iterators.

multimap <datatype, datatype> newMmap (oldMmap.begin(), oldMmap.end())

size(): This will return the number of key value pairs in the multimap.

max_size(): This will return the what is the maximum capacity of the multimap

clear(): This will truncate all elements from multimap

empty(): This is a Boolean operation. Returns true if multimap is empty. Returns false if multimap is not empty.

Example program to explain all above functions is:

#include <iostream>
#include <map>
#include <iterator>

using namespace std;

int main()
{
    multimap <int, int> mmp;        // declaring an empty multimap
    multimap <int, int> :: iterator it;
        
        // inserting elements
        for (int i=1; i<=5; i++ ) {
                mmp.insert (pair <int, int> (i, 10*i));
        }
 
    // printing elements in  multimap
        cout << "Elements in multimap (key and value) pair wise" << endl;
        
    for (it = mmp.begin(); it != mmp.end(); it++ ) {
        cout  << it->first <<  '\t' << it->second << endl;
    }
    cout << endl;
 
    // assigning all elements of multimap1 to multimap2
    multimap <int, int> mmp2(mmp.begin(),mmp.end());
 
    //printing elements of multimap2
    cout << "Elements in multimap2 (key and value) pair wise" << endl;
        
    for (it = mmp2.begin(); it != mmp2.end(); it++ ) {
        cout  << it->first <<  '\t' << it->second << endl;
    }
    cout << endl;
 
        cout << "Size of the multimap is " ;
        cout << mmp.size() << endl;
        cout << endl << "Maximum size of the multimap is " ;
        cout << mmp.max_size() << endl;
        
        cout << endl << "Applying clear operation on multimap2..." <<endl;
        mmp2.clear();
        
        cout << endl << "Checking wheter multimap2 is  empty or not using empty() operation" << endl;
        if (mmp2.empty()) {
                cout << "Multimap2 is empty " << endl;
        }
        else {
                cout << "Multimap2 is not empty " << endl;
        }
        
        return 0;
}

Output

Elements in multimap (key and value) pair wise
1 10
2 20
3 30
4 40
5 50

Elements in multimap2 (key and value) pair wise
1 10
2 20
3 30
4 40
5 50

Size of the multimap is 5

Maximum size of the multimap is 461168601842738790

Applying clear operation on multimap2…

Checking wheter multimap2 is empty or not using empty() operation
Multimap2 is empty

Some other operations are:

find(key): This will find all elements with specified key value.

erase(key): This will delete the value with specified key value.

swap(): This operations swaps all key value pairs of multimap1 to multimap2 and same way multimap2 to multimap1.

upper_bound(): This will return the iterator to upper bound.

lower_bound(): This will return the iterator to lower bound.

Example program to show above operations:

#include <iostream>
#include <map>
#include <iterator>

using namespace std;

int main()
{
        multimap <int, int> mmp;
        multimap <int, int> mmp2;
        multimap <int, int> :: iterator it;
        multimap <int, int> :: iterator it1;
        multimap <int, int> :: iterator it2;
        
        for (int i=1; i<=5; i++ ) {
                mmp.insert (pair <int, int> (i, 10*i));
        }
 
        for (int i=1; i<=5; i++ ) {
                mmp2.insert (pair <int, int> (i*3, i*i*i));
        }
        
        cout << "Elements in multimap1 before swapping are" << endl;
        
        for (it = mmp.begin(); it != mmp.end(); it++ ) {
                cout  << it->first <<  '\t' << it->second << endl;
        }
        
        cout << endl;
        cout << "Elements in multimap2 before swapping are" << endl;
        for (it = mmp2.begin(); it != mmp2.end(); it++ ) {
                cout  << it->first <<  '\t' << it->second << endl;
        }
        
        cout << endl;
        cout << "Performing swapping operation......" << endl;
        mmp.swap(mmp2);
        cout << "Elements in multimap1 after swapping are" << endl;
        
        for (it = mmp.begin(); it != mmp.end(); it++ ) {
                cout  << it->first <<  '\t' << it->second << endl;
        }
        
        cout << endl;
        cout << "Elements in multimap2 after swapping are" << endl;
        for (it = mmp2.begin(); it != mmp2.end(); it++ ) {
                cout  << it->first <<  '\t' << it->second << endl;
        }
        
        cout << endl;
        it= mmp.begin();
        mmp.erase (it); // erasing first element of the mmp
        
        cout << "After erasing first element in multimap1" << endl;
        for (it = mmp.begin(); it != mmp.end(); it++ ) {
                cout  << it->first <<  '\t' << it->second << endl;
        }
        
        cout << "\nUsing lower bound and upper bound for printing" << endl;
        it1= mmp2.lower_bound(2);
        it2= mmp2.upper_bound(4);
        for (it = it1; it != it2; it++ ) {
                cout  << it->first <<  '\t' << it->second << endl;
        }
        
        return 0;
}

Output

Elements in multimap1 before swapping are
1 10
2 20
3 30
4 40
5 50

Elements in multimap2 before swapping are
3 1
6 8
9 27
12 64
15 125

Performing swapping operation……
Elements in multimap1 after swapping are
3 1
6 8
9 27
12 64
15 125

Elements in multimap2 after swapping are
1 10
2 20
3 30
4 40
5 50

After erasing first element in multimap1
6 8
9 27
12 64
15 125

Using lower bound and upper bound for printing
2 20
3 30
4 40

Comment below if you have any queries related to above stl multimap or std::multimap tutorial.

The post C++ STL Multimap Container – std::multimap appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/10/stl-multimap.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do