multimap::begin() and multimap::end() in C++ STL
- multimap::begin() is a built-in function in C++ STL which returns an iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container’s sorting criterion.
Syntax:
multimap_name.begin()
Parameters: The function does not accept any parameter.
Return Value: The function returns an iterator referring to the first element in the multimap container
// C++ function to illustrate// the multimap::begin() function#include <bits/stdc++.h>usingnamespacestd;intmain(){// initialize containermultimap<int,int> mp;// insert elements in random ordermp.insert({ 2, 30 });mp.insert({ 1, 40 });mp.insert({ 3, 60 });mp.insert({ 4, 20 });mp.insert({ 5, 50 });autoite = mp.begin();cout <<"The first element is: ";cout <<"{"<< ite->first <<", "<< ite->second <<"}\n";// prints the elementscout <<"\nThe multimap is : \n";cout <<"KEY\tELEMENT\n";for(autoitr = mp.begin(); itr != mp.end(); ++itr) {cout << itr->first<<'\t'<< itr->second <<'\n';}return0;}chevron_rightfilter_noneOutput:The first element is: {1, 40} The multimap is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50 - multimap::end() is a built-in function in C++ STL which returns an iterator to the theoretical element that follows last element in the multimap. Since multimap container contains the element in an ordered way, end() will point to that theoretical position which follows the last element according to the container’s sorting criterion.
Syntax:
multimap_name.end()
Parameters: The function does not accept any parameter.
Return Value: The function returns an iterator referring to the first element in the multimap container
// C++ function to illustrate// the multimap::end() function#include <bits/stdc++.h>usingnamespacestd;intmain(){// initialize containermultimap<int,int> mp;// insert elements in random ordermp.insert({ 2, 30 });mp.insert({ 1, 40 });mp.insert({ 3, 60 });mp.insert({ 4, 20 });mp.insert({ 5, 50 });// prints the elementscout <<"\nThe multimap is : \n";cout <<"KEY\tELEMENT\n";for(autoitr = mp.begin(); itr != mp.end(); ++itr) {cout << itr->first<<'\t'<< itr->second <<'\n';}return0;}chevron_rightfilter_noneOutput:The multimap is : KEY ELEMENT 1 40 2 30 3 60 4 20 5 50
Recommended Posts:
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
- Machine Learning in C++
- How can we use Comma operator in place of curly braces?
- Optimally accommodate 0s and 1s from a Binary String into K buckets
- Remove first adjacent pairs of similar characters until possible
- Find a number containing N - 1 set bits at even positions from the right
- Sort an array according to absolute difference with given value using Functors
- Rearrange characters in a string such that no two adjacent are same using hashing
- Remove duplicate elements in an Array using STL in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



