C++ Map Library - clear() Function
Description
The C++ function std::map::clear() destroys the map by removing all elements and sets size of map to zero.
Declaration
Following is the declaration for std::map::clear() function form std::map header.
C++98
void clear();
C+11
void clear() noexcept;
Parameters
None
Return value
None
Exceptions
This member function never throws exception.
Time complexity
Linear i.e. O(n)
Example
The following example shows the usage of std::map::clear() function.
#include <iostream>
#include <map>
using namespace std;
int main(void) {
/* Initializer_list constructor */
map<char, int> m = {
{'a', 1},
{'b', 2},
{'c', 3},
{'d', 4},
{'e', 5},
};
cout << "Initial size of map = " << m.size() << endl;
m.clear();
cout << "Size of map after clear opearation = " << m.size() << endl;
return 0;
}
Let us compile and run the above program, this will produce the following result −
Initial size of map = 5 Size of map after clear opearation = 0
map.htm
Advertisements