C++ Map Library - clear() Function



Description

The C++ function std::multimap::clear() destroys the multimap by removing all elements and sets size of multimap to zero.

Declaration

Following is the declaration for std::multimap::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::multimap::clear() function.

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   /* Multimap with duplicates */
   multimap<char, int> m {
         {'a', 1},
         {'a', 2},
         {'b', 3},
         {'c', 4},
         {'c', 5},
               };

   cout << "Initial size of multimap = " << m.size() << endl;

   m.clear();

   cout << "Size of multimap after clear operation = "
        << m.size() << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Initial size of multimap = 5
Size of multimap after clear operation = 0
map.htm
Advertisements