
- 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::cbegin() and multimap::cend() in C++ STL
In this article we will be discussing the working, syntax and examples of multimap::cbegin() and multimap::cend() functions 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::cbegin()?
multimap::cbegin() function is an inbuilt function in C++ STL, which is defined in <map> header file. cbegin() is the constant begin function. This function returns the constant iterator which is pointing to the first element in the multimap container. The iterator returned is the constant iterator, they can’t be used to modify the content.We can use them to traverse among the elements of the map container by increasing or decreasing the iterator.
Syntax
multi.cbegin();
Parameters
This function accepts no parameter.
Return value
It returns an iterator pointing to the first element of the associated map container.
Input
multimap<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.cbegin();
Output −
a = 1
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); auto it = mul.cbegin(); cout << "First element in the multimap is: "; cout << "{" << it->first << ", " << it->second << "}\n"; cout << "\nElements in multimap is : \n"; cout << "KEY\tELEMENT\n"; for (auto i = mul.cbegin(); i!= mul.cend(); i++){ cout << i->first << "\t" << i->second << endl; } return 0; }
Output
If we run the above code it will generate the following output −
First element in the multimap is: {1, 50} Elements in multimap is : KEY ELEMENT 1 50 1 40 1 10 2 30 2 20 5 60
What is multimap::cend()?
multimap::cend() function is an inbuilt function in C++ STL, which is defined in <map> header file. cend() function is the constant end (). This function returns the constant iterator of the element which is past the last element in the associated multimap container.
The iterator returned is the constant iterator, they can’t be used to modify the content. We can use them to traverse among the elements of the map container by increasing or decreasing the iterator.
multimap::cbegin() and multimap::cend() are used to traverse through the whole container by giving a start of the range and end of the range.
Syntax
multi.cend();
Parameters
This function accepts no parameter.
Return value
It returns an iterator pointing to the past the last element of the associated map container.
Input −
multimap <char, int> newmap; newmap(make_pair(‘a’, 1)); newmap(make_pair(‘b’, 2)); newmap(make_pair(‘c’, 3)); newmap.cend();
Output −
error
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.cbegin(); i!= mul.cend(); i++){ cout << i->first << "\t" << i->second << endl; } 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
- Related Articles
- multimap::begin() and multimap::end() in C++ STL
- multimap::crbegin() and multimap::crend() in C++ STL
- Set cbegin() and cend() function in C++ STL
- list cbegin() and cend() functions in C++ STL
- map cbegin() and cend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- match_results cbegin() add 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
