Multimap insert(), erase() in C++ STL

C++ STL | Multimap insert(), erase(): In this tutorial, we are going to see how to insert and delete in multimap C++ STL along with its application and use cases?
Submitted by Radib Kar, on June 18, 2020

C++ multimap::insert() and multimap::erase() functions

Multimap is used when you need to store the same keys with distinct values where the map fails to do the same.

In this article, we are going to see how to insert and delete in multimap?

Multimap also stores the keys in sorted order and has the same time complexity as the map.

To declare a multimap,

multiplate <int,int> mymap;

Multimap Insert()

The insert() function is not similar to the map. In the map, we insert like an array by using the key as an index.

In multimap, you need to insert <key, value> pair as a pair.

So the syntax is,

Iterator insert(pair<key,value>);

Parameter(s): Pair of key, value pair.

Return value: Iterator to the inserted element.

Below is an example of insertion,

Let's insert the below <key, value> pairs,

Key	Value
5	10
2	8
3	12
2	14
5	6

The above insertion will be like below,

1)  Declare the multimap
    multimap <int,int> mymap;
2)  Insert the first key
    mymap.insert(make_pair(5,10));
3)  Insert the first key
    mymap.insert(make_pair(2,8));
4)  Insert the first key
    mymap.insert(make_pair(3,12));
5)  Insert the first key
    mymap.insert(make_pair(2,14));
6)  Insert the first key
    mymap.insert(make_pair(5,6));

After all the insertion the multimap will be following,

Key	Value
2	8
2	14
3	12
5	10
5	6

Multimap erase()

The erase function is quite similar to the map. The function has three kinds of usage:

1. Erase all the entries of the key

In multimap, you need to provide the key for the pair to delete. It will delete all occurrences of the key in the multimap.

So the syntax is,

erase(key);

Parameter(s): key

Return value: void (Simply deletes all the occurrences)

Below is an example of deletion,

Let's delete entries for key=2

Multimap is already constructed by the insert function

Now,
    mymap.erase(2)

This will erase both the entries with the key 

Resultant map would be:

Key	Value
3	12
5	10
5	6

2. Erasing only a single value based on position

In multimap, you need to provide the position for the pair to delete. It will delete at the exact position in the multimap.

So the syntax is,

erase(int position);

Parameter(s): position

Return value: void (Simply deletes at the position)

Below is an example of such deletion,

Let's delete the first entry of the multimap 
(after previous deletion what we got)

    mymap.erase(mymap.begin())

It would delete the entry with key 3. 
So the resultant multimap would be

Key	Value
5	10
5	6

3. Deletion in range

There is a third kind of deletion which is based on deletes. It deletes entries within the range.

mymap.erase(iterator position1, iterator position2)

Parameter(s): starting and ending iterators to define a range (the first iterator is inclusive and the last one is exclusive)

Return value: void (Simply deletes within the range)

Example is provided in the code and output

C++ implementation of multimap insert(), erase() functions

#include <bits/stdc++.h>
using namespace std;

int main()
{
    multimap<int, int> mymultimap;
    //insertion in multimap

    cout << "Inserting like above example\n";
    mymultimap.insert(make_pair(5, 10));
    mymultimap.insert(make_pair(2, 8));
    mymultimap.insert(make_pair(3, 12));
    mymultimap.insert(make_pair(2, 14));
    mymultimap.insert(make_pair(5, 6));

    cout << "Printing the multimap\n";
    multimap<int, int>::iterator ij;
    for (ij = mymultimap.begin(); ij != mymultimap.end(); ij++) {
        cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    }

    cout << "deleting key 2\n";
    mymultimap.erase(2);
    cout << "Printing the multimap after deletion\n";

    for (ij = mymultimap.begin(); ij != mymultimap.end(); ij++) {
        cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    }

    cout << "deleting the first position entry\n";
    mymultimap.erase(mymultimap.begin());

    cout << "Printing the multimap after deletion\n";

    for (ij = mymultimap.begin(); ij != mymultimap.end(); ij++) {
        cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    }

    cout << "deleting the total range\n";
    mymultimap.erase(mymultimap.begin(), mymultimap.end());

    cout << "Printing the multimap after deletion\n";

    for (ij = mymultimap.begin(); ij != mymultimap.end(); ij++) {
        cout << "key: " << ij->first << " ,value: " << ij->second << endl;
    }
 
    if (mymultimap.size() == 0) {
        cout << "the multimap is deleted\n";
    }

    return 0;
}

Output

Inserting like above example
Printing the multimap
key: 2 ,value: 8
key: 2 ,value: 14
key: 3 ,value: 12
key: 5 ,value: 10
key: 5 ,value: 6
deleting key 2
Printing the multimap after deletion
key: 3 ,value: 12
key: 5 ,value: 10
key: 5 ,value: 6
deleting the first position entry
Printing the multimap after deletion
key: 5 ,value: 10
key: 5 ,value: 6
deleting the total range
Printing the multimap after deletion
the multimap is deleted



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.