
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
map count( ) function in C++
In this article we will be discussing the working, syntax and examples of map::empty() function in C++ STL.
What is Map in C++ STL?
Maps are the associative container, which facilitates to store the elements formed by a combination on 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 map container is accessed by its unique keys.
What is map::count()?
The map::count( ) is a function which comes under <map> header file. This function counts the elements with specific key, it returns 1 if the element with key is present, It returns the 0 if the element with key is not present in container.
Syntax
map_name.count(key n);
Parameter
This function accepts the parameter N which specifies Key in container.
Returns
This function returns the Boolean Number 1 if the key is present in the container and returns 0 if the key is not present in the container.
Input (key, Element)
(2,70), (3,30), (4,90), (5,100)
Output
Key 5 is present. Key 6 is not present. 2,11 3,26 1,66 4,81 Key 2 is present. Key 8 is not present.
Approach can be followed
First initialize the container.
Then insert the element with their keys.
Then we check the desired key is present in the container.
By using the above approach, we can check the key is present in the container, there is another approach which can be followed as −
Firstly, we initialize the container.
Then we insert the element with their keys.
Then we create the loop from first element to last element.
In this loop, we check the the desired key is present or not.
Above approach is generally used for alphabetically stored element. In this approach code print that the element is present in element or not in Output.
Example
/ / C++ code to demonstrate the working of map count( ) function #incude<iostream.h> #include<map.h> Using namespace std; int main( ){ map<int, int> mp; mp.insert({1, 40}); mp.insert({3, 20}); mp.insert({2, 30}); mp.insert({5, 10}); mp.insert({4, 50}); if (mp.count(1)) cout<< ” The Key 1 is present\n”; else cout<< ” The Key 1 is not present\n”; if(mp.count(7)) cout<< “ The Key 7 is Present \n”; else cout<< “ The Key 7 is not Present\n”; return 0; }
Output
If we run the above code it will generate following output
The Key 1 is present The Key 7 is not present
Example
#include<iostream.h> #include<map.h> Using namespace std; int main ( ){ map<char, int> mp; int I; mp[‘a’] = 2; mp[‘c’] = 3; mp[‘e’] = 1; for ( i = ’a’ ; i < ’f’ ; i++){ cout<< i; if (mp.count(i)>0) cout<< “ is an element of mp.\n”; else cout<< “ is not an element of mp.\n”; } return 0; }
Output
If we run the above code it will generate the following output
a is an element of mp. b is not an element of mp. c is an element of mp. d is not an element of mp. e is an element of mp. f is not an element of mp.
- Related Articles
- map emplace_hint() function in C++ STL
- map erase() function in C++ STL
- map find() function in C++ STL
- map key_comp() function in C++ STL
- map rbegin() function in C++ STL
- map rend() function in C++ STL
- map lower_bound() function in C++ STL
- map upper_bound() function in C++ STL
- map cbegin() and cend() function in C++ STL
- map crbegin() and crend() function in C++ STL
- multiset count() function in C++ STL
- Set count() function in C++ STL
- map::at() and map::swap() in C++ STL
- Sum 2D array in Python using map() function
- count() function in PHP
