
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Map Size in C++ STL
In this article we will be discussing the working, syntax and examples of map::size() function in C++ STL.
What is a Map in C++ STL?
Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.
What is a map::size()?
map::size() function is an inbuilt function in C++ STL, which is defined in
Syntax
map_name.size();
Parameters
The function accepts no parameter.
Return value
This function returns the number of elements in the map container. If the container has no values the function returns 0.
Example
Input
std::map<int> mymap; mymap.insert({‘a’, 10}); mymap.insert({‘b’, 20}); mymap.insert({‘c’, 30}); mymap.size();
Output
3
Input
std::map<int> mymap; mymap.size();
Output
0
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_1; TP_1[1] = 10; TP_1[2] = 20; TP_1[3] = 30; TP_1[4] = 40; cout<<"Size of TP_1 is: "<<TP_1.size(); return 0; }
Output
Size of TP_1 is: 4
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_1; TP_1[1] = 10; TP_1[2] = 20; TP_1[3] = 30; TP_1[4] = 40; auto size = TP_1.size(); auto temp = 1; while(size!=0) { temp = temp * 10; size--; } cout<<"Temp value is: "<<temp<<endl; return 0; }
Output
Temp value is: 10000
Advertisements