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

map::size() in C++ STL

Last Updated : 27 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report


Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values.

map::size()
In C++, size() function is used to return the total number of elements present in the map.

Syntax:

map_name.size()

Return Value: It returns the number of elements present in the map.

Examples:

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

Input : map2 = {};
        map2.size();
Output: 0




// C++ program to illustrate
// implementation of size() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    // Take any two maps
    map<int, string> map1, map2;
      
    // Inserting values
    map1.insert(make_pair(1, "India"));
    map1.insert(make_pair(2, "Nepal"));
    map1.insert(make_pair(3, "Sri Lanka"));
    map1.insert(make_pair(4, "Myanmar"));
      
    // Printing the size
    cout << "map1 size: " << map1.size();
    cout << endl;
    cout << "map2 size: " << map2.size();
    return 0;
}


Output:

map1 size: 4
map2 size: 0

Time complexity: Constant i.e. O(1)


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
Searching in a map using std::map functions in C++
Usually, main purpose of using map stl container is for efficient search operations and sorted order retrieval. As map stores key-value pair, all the search operations take "O(log(n))" time (n is size of map). Different types of search functions exists in C++ language, each having different functions. In the context of competitive programming, this
7 min read
map::clear() in C++ STL
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 ar
2 min read
map::begin() and end() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::begin() begin() function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first elemen
3 min read
map::empty() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::empty() empty() function is used to check if the map container is empty or not. Syntax : mapname.empty() Parameters : No parameters are passed. Returns : True, if map is empty
1 min read
map::operator[] in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::operator[] This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at(
2 min read
Default values in a Map in C++ STL
Prerequisite: Map in STLA map is a container which is used to store a key-value pair. By default, In Primitive datatypes such as int, char, bool, float in C/C++ are undefined if variables are not initialized, But a Map is initially empty when it is declared. When this map is accessed with the [ ] (e.g map&lt;int,int&gt; mpp; mpp[1]; ) if the key is
2 min read
set vs map in C++ STL
set and map in STL are similar in the sense that they both use Red Black Tree (A self balancing BST). Note that the time complexities of search, insert and delete are O(Log n). Differences: The difference is set is used to store only keys while map is used to store key value pairs. For example consider in the problem of printing sorted distinct ele
4 min read
map emplace() in C++ STL
The map::emplace() is a built-in function in C++ STL which inserts the key and its element in the map container. It effectively increases the container size by one. If the same key is emplaced more than once, the map stores the first element only as the map is a container which does not store multiple keys of the same value. Syntax: map_name.emplac
2 min read
map cbegin() and cend() function in C++ STL
map::cbegin() is a built-in function in C++ STL that returns a constant iterator referring to the first element in the map container. Since map container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: map_name.cbegin()Parameters: The function d
3 min read
Article Tags :
Practice Tags :