×

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.

Delete elements from a Map | C++ STL

C++ STL | std::map::erase() and std::map::clear() functions with Example: Here, we are going to learn how to delete elements from a Map in C++ STL?
Submitted by Vivek Kothari, on December 15, 2018

We can use std::map::erase() function to erase single element or range of elements in a map. If we want to delete all the elements in a map we can use std::map::clear() function. Let discuss these functions in detail.

std::map::erase() function

It removes the element from a map and reduces the size of a map by 1.

Syntax

//Erase by using iterator: 
MapName.erase (it);
//Where ‘it’ is iterator of type map.

//Erasing by key :
MapName.erase (key);

//Erasing by range :
MapName.erase (first,last);
//Where the range includes all the elements of the map 
//between first and lasti.e[first,last).

Example to delete elements from a Map using std::map::erase() function

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

int main() {
  map<char, int> MyMap;

  // insert some elements in map
  MyMap['a'] = 1;
  MyMap['b'] = 2;
  MyMap['c'] = 3;
  MyMap['d'] = 4;
  MyMap['e'] = 5;
  MyMap['f'] = 6;
  MyMap['g'] = 7;

  map<char, int>::iterator it;

  cout << "Elements in map : " << endl;
  for (it = MyMap.begin(); it != MyMap.end(); it++) {
    cout << "key = " << it->first << " value = " << it->second << endl;
  }
  cout << endl;

  // delete using key
  MyMap.erase('f');

  // erasing by iterator
  it = MyMap.find('e');
  MyMap.erase(it);

  cout << "Elements in map after deleting f and e: " << endl;
  for (it = MyMap.begin(); it != MyMap.end(); it++) {
    cout << "key = " << it->first << " value = " << it->second << endl;
  }
  cout << endl;

  // erasing by range
  // note that last is not inclusive
  // delete elements from a to c
  it = MyMap.find('d');
  MyMap.erase(MyMap.begin(), it);

  cout << "Elements in map after deleting a to c : " << endl;
  for (it = MyMap.begin(); it != MyMap.end(); it++) {
    cout << "key = " << it->first << " value = " << it->second << endl;
  }

  return 0;
}

Output

Elements in map :
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7

Elements in map after deleting f and e:
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = g value = 7

Elements in map after deleting a to c :
key = d value = 4
key = g value = 7

std::map::clear() function

This function removes all elements from a map and reduces map size to 0.

Syntax

MapName.clear();
//There is no parameter to pass.

Delete elements from a Map using std::map::clear() function

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

int main() {
  map<char, int> MyMap;

  // insert some elements in map
  MyMap['a'] = 1;
  MyMap['b'] = 2;
  MyMap['c'] = 3;
  MyMap['d'] = 4;
  MyMap['e'] = 5;
  MyMap['f'] = 6;
  MyMap['g'] = 7;

  map<char, int>::iterator it;

  cout << "Elements in map : " << endl;
  for (it = MyMap.begin(); it != MyMap.end(); it++) {
    cout << "key = " << it->first << " value = " << it->second << endl;
  }
  cout << endl;

  // erasing all elements
  MyMap.clear();

  cout << "After applying MyMap.clear() function : " << endl;
  if (MyMap.size() == 0) cout << "map is empty !!!";

  return 0;
}

Output

Elements in map :
key = a value = 1
key = b value = 2
key = c value = 3
key = d value = 4
key = e value = 5
key = f value = 6
key = g value = 7

After applying MyMap.clear() function :
map is empty !!!

Comments and Discussions!

Load comments ↻





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