×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

unordered_map erase() Function in C++ with Examples

C++ unordered_map erase() Function: Here, we will learn about the erase() function, its usages, syntax and examples.
Submitted by Shivang Yadav, on July 20, 2022

An unordered_map is a special type of container that stores data in the form of key-value pairs in an unordered manner. The key stored is used to identify the data value mapped to it uniquely.

C++ STL std::unordered_map::erase() Function

The erase() function is used to erase elements from the given unordered_map.

  • A specific key-value pair can be erased from an unorder_map based on the key-value or from a specific position.
  • Multiple pairs can be removed based on the range.

Erasing value from a specific Position [erase by iterator]

The erase() function can be used to erase a value from a specific position. This is done by passing an iterator as a parameter to the function.

Syntax

unordered_map. erase (iterator it);

Parameter(s)

It accepts a single parameter which is the iterator.

Return value

It returns nothing.

Example 1

#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
    unordered_map<int, char> myMap = { { 1, 'a' }, { 5, 'x' }, { 9, 's' } };

    cout << "Size of unordered_map :" << myMap.size() << endl;
    cout << "The elements of unordered_map are ";
    for (auto x : myMap)
        cout << x.first << " : " << x.second << "\t";
    cout << endl;

    cout << "After erasing first element by Iterator\n";
    myMap.erase(myMap.begin());
    cout << "Size of unordered_map :" << myMap.size() << endl;
    cout << "The elements of unordered_map are ";
    for (auto x : myMap)
        cout << x.first << " : " << x.second << "\t";
    cout << endl;

    return 0;
}

Output

Size of unordered_map :3
The elements of unordered_map are 9 : s 5 : x   1 : a
After erasing first element by Iterator
Size of unordered_map :2
The elements of unordered_map are 5 : x 1 : a

Erasing pair based on key

A pair can be erased based on the key value. This is done by passing the key as a parameter to the method.

Syntax:

unordered_map. erase (key-value);

Parameter(s): It accepts a single parameter which is the value of the key.

Return Value: It returns nothing.

Example 2

#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
    unordered_map<int, char> myMap = { { 1, 'a' }, { 5, 'x' }, { 9, 's' } };

    cout << "Size of unordered_map :" << myMap.size() << endl;
    cout << "The elements of unordered_map are ";
    for (auto x : myMap)
        cout << x.first << " : " << x.second << "\t";
    cout << endl;

    cout << "After erasing first element by key\n";
    myMap.erase(5);
    cout << "Size of unordered_map :" << myMap.size() << endl;
    cout << "The elements of unordered_map are ";
    for (auto x : myMap)
        cout << x.first << " : " << x.second << "\t";
    cout << endl;

    return 0;
}

Output

Size of unordered_map :3
The elements of unordered_map are 9 : s 5 : x   1 : a
After erasing first element by key
Size of unordered_map :2
The elements of unordered_map are 9 : s 1 : a

Erasing by range

Multiple pairs within a range can be deleted using the range. This is done by passing two iterators of range as parameters.

Syntax:

unordered_map.erase(startIterator , endIterator);

Parameter(s): It accepts two parameters both iterators for the start and end of the range.

Return Value: It returns nothing.

Example 3

#include <iostream>
#include <unordered_map>
using namespace std;

int main()
{
    unordered_map<int, char> myMap = { { 1, 'a' }, { 5, 'x' }, { 9, 's' } };

    cout << "Size of unordered_map :" << myMap.size() << endl;
    cout << "The elements of unordered_map are ";
    for (auto x : myMap)
        cout << x.first << " : " << x.second << "\t";
    cout << endl;

    cout << "After erasing first elements of a range \n";
    auto sIter = myMap.begin();
    sIter++;
    auto eIter = myMap.end();
    myMap.erase(sIter, eIter);
    cout << "Size of unordered_map :" << myMap.size() << endl;
    cout << "The elements of unordered_map are ";
    for (auto x : myMap)
        cout << x.first << " : " << x.second << "\t";
    cout << endl;

    return 0;
}

Output

Size of unordered_map :3
The elements of unordered_map are 9 : s 5 : x   1 : a
After erasing first elements of a range 
Size of unordered_map :1
The elements of unordered_map are 9 : s

Comments and Discussions!

Load comments ↻





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