C++ Map Library - size() Function


Advertisements

Description

The C++ function std::multimap::size() returns the number of elements present in the multimap.

Declaration

Following is the declaration for std::multimap::size() function form std::map header.

C++98

size_type size() const;

C++11

size_type size() const noexcept;

Parameters

None

Return value

Returns the actual objects present in multimap.

Exceptions

This member function never throws exception.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::multimap::size() 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},
            {'d', 5}
         };

   cout << "Multimap contains " << m.size() << " elements." << endl;

   return 0;
}

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

Multimap contains 5 elements.
map.htm
Advertisements