CPP Map: Erase by Value or Callback while Iterating | erase_if for Map

In the previous article, we have discussed aboutĀ Convert First Letter of Each Word of a String to Upper Case in CPP. Let us learn About Map: Erase by Value or Callback while Iterating in C++ Program.

Maps:

C++ STL mapping is Maps (Standard Template Library). Maps are the associative containers in which and key is special and can be inserted or delected, but cannot be altered. Key-related values can be modified.

In this article, we will look at two different methods for removing map elements.

Erasing map elements

There are several ways to erase from map some of them are:

Method #1:Erasing elements by Value

Algorithm:

  • Identify all of the elements and remove the elements (Key value pairs) that match the value in the sector.
  • A template function is created which works for all maps, e.g. std::map<Key, Value> where K & V can be anything.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
template <typename K, typename V>
int erase_Value(std::map<K, V>& elementmap, V given_value)
{
    int number_ofDeletedElements = 0;
    auto itr = elementmap.begin();
    // Traverse the map
    while (itr != elementmap.end()) {
        // checking if the value matches with the given
        // value
        if (itr->second == given_value) {
            number_ofDeletedElements++;
            // erase the element
            itr = elementmap.erase(itr);
        }
        else {
            // increment the iterator
            itr++;
        }
    }
    // return the number of deleted elements
    return number_ofDeletedElements;
}
int main()
{
    // creating a map with string as key and integers as
    // value
    map<std::string, int> elementmap
        = { { "hello", 100 },
            { "this", 200 },
            { "is", 100 },
            { "BTechGeeks", 300 } };

    cout << "Printing the map before deleting " << endl;
    // Printing the map elements
    for (auto element : elementmap)
        cout << element.first << " :: " << element.second
             << endl;
    int givenval = 100;
    // Erasing the elements with the given value
    int deleted_elements_count
        = erase_Value(elementmap, givenval);
    cout << "Number of elements deleted = "
         << deleted_elements_count << endl;

    cout << "Printing the map after deleting " << endl;
    // Print the map elements
    // Printing the map elements
    for (auto element : elementmap)
        cout << element.first << " :: " << element.second
             << endl;
    return 0;
}

Output:

Printing the map before deleting : 
BTechGeeks :: 300
hello :: 100
is :: 100
this :: 200
Number of elements deleted = 2
Printing the map after deleting :
BTechGeeks :: 300
this :: 200

Method #2:Erasing elements using Callback

Algorithm:

  • Iterate through all of the elements, calling the given callback for each one. If the callback returns true, the element is deleted and the process moves on to the next.
  • We’ll write a template function that can operate for any kind of map, such as std::map<K, V>, where K and V can be anything.

Assume we have a string and int map. Delete all elements with even values.

Below is the implementation:

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

template <typename K, typename V>
int eraseValue(std::map<K, V>& elementMap,
               bool (*functor)(V))
{
    int number_ofDeletedElements = 0;
    auto itr = elementMap.begin();
    // Traverse the map
    while (itr != elementMap.end()) {
        // checking if the value matches with the given
        // value
        if (functor(itr->second)) {
            number_ofDeletedElements++;
            // erase the element
            itr = elementMap.erase(itr);
        }
        else {
            // Go to next entry in map
            itr++;
        }
    }
    return number_ofDeletedElements;
}
bool isEven(int value)
{
    if (value % 2 == 0)
        return true;
    else
        return false;
}
int main()
{
    // creating a map with string as key and integers as
    // value
    map<std::string, int> elementmap
        = { { "hello", 23 },
            { "this", 46 },
            { "is", 98 },
            { "BTechGeeks", 243 } };
    cout << "Printing the map before deleting : " << endl;
    // Printing the map elements
    for (auto element : elementmap)
        cout << element.first << " :: " << element.second
             << endl;

    // erasing all elements from map  if the value is even
    int deleted_elements_count
        = eraseValue(elementmap, &isEven);
    cout << "Number of elements deleted = "
         << deleted_elements_count << endl;
    cout << "Printing the map after deleting  : " << endl;
    // Print the map elements
    // Printing the map elements
    for (auto element : elementmap)
        cout << element.first << " :: " << element.second
             << endl;
    return 0;
}

Output:

Printing the map before deleting : 
BTechGeeks :: 243
hello :: 23
is :: 98
this :: 46
Number of elements deleted = 2
Printing the map after deleting  : 
BTechGeeks :: 243
hello :: 23

Related Programs: