C++ map size() Function
Example
Get the number of elements in a map:
map<string, int> people = {{"John", 32}, {"Adele", 45}, {"Bo", 29}};
cout << people.size();
Definition and Usage
The size() function returns the number of elements in a map.
The size function is defined in the <map> header file.
Tip: Use empty() to check if a map has no elements.
Syntax
map.size()
Parameter Values
None.
Technical Details
| Returns: | The number of elements in the map (a non-negative integer). |
|---|
Related Pages
Read more about maps in our Maps Tutorial.