The Wayback Machine - https://web.archive.org/web/20240606091606/https://www.geeksforgeeks.org/mapclear-c-stl/
Open In App

map::clear() in C++ STL

Last Updated : 20 Jan, 2018
Improve
Improve
Like Article
Like
Save
Share
Report


Map is dictionary like data structure. It is an associative array of (key, value) pair, where only single value is associated with each unique key.

map::clear()
clear() function is used to remove all the elements from the map container and thus leaving it’s size 0.

Syntax:

map1.clear()
where map1 is the name of the map.

Parameters:
No parameters are passed.

Return Value: None

Examples:

Input : map1 = { 
                {1, "India"},
                {2, "Nepal"},
                {3, "Sri Lanka"},
                {4, "Myanmar"}
               }
        map1.clear();
Output: map1 = {}

Input : map2 = {}
        map2.clear();
Output: map2 = {}




// CPP program to illustrate
// Implementation of clear() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Take any two maps
    map<int, string> map1, map2;
      
    // Inserting values
    map1[1] = "India";
    map1[2] = "Nepal";
    map1[3] = "Sri Lanka";
    map1[4] = "Myanmar";
      
    // Print the size of map
    cout<< "Map size before running function: \n";
    cout << "map1 size = " << map1.size() << endl;
    cout << "map2 size = " << map2.size() << endl;;
      
    // Deleting the map elements
    map1.clear();
    map2.clear();
      
    // Print the size of map
    cout<< "Map size after running function: \n";
    cout << "map1 size = " << map1.size() << endl;
    cout << "map2 size = " << map2.size();
    return 0;
}


Output:

Map size before running function: 
map1 size = 4
map2 size = 0
Map size after running function: 
map1 size = 0
map2 size = 0

Time complexity: Linear i.e. O(n)


Previous Article
Next Article

Similar Reads

map::at() and map::swap() in C++ STL
Maps are the container in STL which is used to store the elements in the form of key-value pair. Internally, the elements in a map are always sorted by its key. Maps are mainly implemented as binary search trees. map::at() at() function is used to return the reference to the element associated with the key k. Syntax: map1.at(k) Parameters: k is the
3 min read
Different ways to delete elements in std::map (erase() and clear())
This article deals with the deletion part of Maps. We can delete elements in std::map using two functions 1. Using erase() The erase() is used to erase the pair in the map mentioned in the argument, either its position, its value, or a range of numbers. We can use the erase function in the following ways: A. Remove using the Keyerase (key) Erases t
4 min read
deque::clear() and deque::erase() in C++ STL
Deque or Double-ended queues are sequence containers with the feature of expansion and contraction on both ends. They are similar to vectors, but are more efficient in the case of insertion and deletion of elements at the end, and also at the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::clear() The clear()
5 min read
list::clear() in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::clear()clear() function is used to remove all the elements of the list container, thus making
1 min read
forward_list::clear() and forward_list::erase_after() in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps tr
4 min read
set::clear in C++ STL
Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() function is used to remove all the elements of the
2 min read
multimap clear() function in C++ STL
The multimap clear() function is an inbuilt function in C++ STL which is used to remove all elements from the multimap container (which are destroyed), leaving the container with a size of 0. Syntax : mymultimap_name.clear() Parameters: This function does not take any arguments. Return Value: This function does not returns anything. The return type
2 min read
multiset clear() function in C++ STL
The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multiset container after removal is 0. Syntax: multiset_name.clear() Parameters: The function does not accept any parameter. Return Value: The function does not returns anything. Below programs illustrates the m
2 min read
unordered_set clear() function in C++ STL
The unordered_set::clear() function is a built-in function in C++ STL which is used to clear an unordered_set container. That is, this function removes all of the elements from an unordered_set and empties it. All of the iterators, pointers, and references to the container are invalidated. This reduces the size of the container to zero. Syntax: uno
2 min read
unordered_multiset clear() function in C++ STL
The unordered_multiset::clear() is a built-in function in C++ STL which clears the contents of the unordered_multiset container. The final size of the container after the call of the function is 0. Syntax: unordered_multiset_name.clear() Parameters: The function does not accept any parameter. Return Value: It returns nothing. Below programs illustr
2 min read
Article Tags :
Practice Tags :