
- 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
multimap::count() in C++ STL
In this article we will be discussing the working, syntax, and examples of multimap::count() function in C++ STL.
What is Multimap in C++ STL?
Multimaps are the associative containers, which are similar to map containers. It also facilitates to store the elements formed by a combination of key-value and mapped value in a specific order. In a multimap container, there can be multiple elements associated with the same key. The data is internally always sorted with the help of its associated keys.
What is multimap::count()?
Multimap::count() function is an inbuilt function in C++ STL, which is defined in <map> header file. count() is used to count the number of elements are present with a specific key in a multimap associated with the function.
This function returns zero if the key is not present in the multimap container.
Syntax
multimap_name.count(key_type& key);
Parameters
The function accepts following parameter(s) −
key − This is the key which we want to search and count the number of elements associated with the key.
Return value
This function returns an integer i.e the number of elements with the same key.
Input
std::multimap<char, int> odd, eve; odd.insert(make_pair(‘a’, 1)); odd.insert(make_pair(‘a, 3)); odd.insert(make_pair(‘c’, 5)); odd.count(‘a’);
Output
2
Example
#include <bits/stdc++.h> using namespace std; int main(){ //create the container multimap<int, int> mul; //insert using emplace mul.emplace_hint(mul.begin(), 1, 10); mul.emplace_hint(mul.begin(), 2, 20); mul.emplace_hint(mul.begin(), 2, 30); mul.emplace_hint(mul.begin(), 1, 40); mul.emplace_hint(mul.begin(), 1, 50); mul.emplace_hint(mul.begin(), 5, 60); cout << "\nElements in multimap is : \n"; cout <<"KEY\tELEMENT\n"; for (auto i = mul.begin(); i!= mul.end(); i++){ cout << i->first << "\t" << i->second << endl; } cout<<"Key 1 appears " << mul.count(1) <<" times in the multimap\n"; cout<<"Key 2 appears " << mul.count(2) <<" times in the multimap\n"; return 0; }
Output
If we run the above code it will generate the following output −
Elements in multimap is : KEY ELEMENT 1 50 1 40 1 10 2 30 2 20 5 60 Key 1 appears 3 times in the multimap Key 2 appears 2 times in the multimap
- Related Articles
- multimap::begin() and multimap::end() in C++ STL
- multimap::crbegin() and multimap::crend() in C++ STL
- multimap::cbegin() and multimap::cend() in C++ STL
- multimap find( ) in C++ STL
- multimap::swap() in C++ STL
- multimap::operator= in C++ STL
- multimap::erase() in C++ STL
- multimap rend in C++ STL
- multimap rbegin in C++ STL
- multimap maxsize() in C++ STL
- multimap insert() in C++ STL
- multimap::emplace() in C++ STL
- multimap::emplace_hint() in C++ STL
- multimap key_comp() in C++ STL
- multimap value_comp() function in C++ STL
