
- 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::begin() and multimap::end() in C++ STL
In this article we will be discussing the working, syntax, and examples of multimap::begin() and multimap::end() functions in C++ STL.
What is Multimap in C++ STL?
Multimaps are the associative containers, which are similar to map containers. It also facilitates storing 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::begin()?
multimap::begin() function is an inbuilt function in C++ STL, which is defined in <map> header file. begin() is used to access the element which is at the very beginning of the associated multimap container.
This function returns an iterator which points to the first element of the container. When the container has no values in it the iterator can’t be deference
Syntax
multimap_name.begin();
Parameters
The function accepts no parameter.
Return value
This function returns an iterator which is pointing to the first value of the map container.
Input
std::multimap<int> mymap; mymap.insert({‘a’, 10}); mymap.insert({‘b’, 20}); mymap.insert({‘c’, 30}); mymap.begin();
Output
a:10
Example
multimap::begin()
#include <bits/stdc++.h> using namespace std; int main(){ multimap<int, int> mul; //inserting elements in multimap mul.insert({ 1, 10 }); mul.insert({ 2, 20 }); mul.insert({ 3, 30 }); mul.insert({ 4, 40 }); mul.insert({ 5, 50 }); //fetching first pair in multimap auto i = mul.begin(); cout<<"First element in multimap is: "; cout << "{" << i->first << ", " << i->second << "}\n"; //displaying multimap elements cout << "\nElements in multimap is : \n"; cout << "KEY\tELEMENT\n"; for (auto it = mul.begin(); it!= mul.end(); ++it){ cout << it->first << '\t' << it->second << '\n'; } return 0; }
Output
IF WE RUN THE ABOVE CODE IT WILL GENERATE THE FOLLOWING OUTPUT −
First element in multimap is: {1, 10} Elements in multimap is : KEY ELEMENT 1 10 2 20 3 30 4 40 5 50
What is multimap::end()?
multimap::end() function is an inbuilt function in C++ STL, which is defined in <map> header file. end() is used to access the element which is at after the last element in the multimap container, or the past the last element.
This function returns an iterator which points to the element which is next to the last element of the container. When the container has no values in it the iterator can’t be dereferenced
Usually begin() and end() are used to iterate through the map container by giving them range.
Syntax
multimap_name.end();
Parameters
The function accepts no parameter.
Return value
If we run the above code it will generate the following output −
Input
std::multimap<int> mymap; mymap.insert({‘a’, 10}); mymap.insert({‘b’, 20}); mymap.insert({‘c’, 30}); mymap.end();
Output
error
Example
multimap::end()
#include <bits/stdc++.h> using namespace std; int main(){ multimap<int, int> mul; //inserting elements in multimap mul.insert({ 1, 10 }); mul.insert({ 2, 20 }); mul.insert({ 3, 30 }); mul.insert({ 4, 40 }); mul.insert({ 5, 50 }); //displaying multimap elements cout << "\nElemenst in multimap is : \n"; cout << "KEY\tELEMENT\n"; for (auto it = mul.begin(); it!= mul.end(); ++it){ cout << it->first << '\t' << it->second << '\n'; } return 0; }
Output
If we run the above code it will generate the following output −
Elements in multimap is : KEY ELEMENT 1 10 2 20 3 30 4 40 5 50
- Related Articles
- multimap::crbegin() and multimap::crend() in C++ STL
- multimap::cbegin() and multimap::cend() 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::count() in C++ STL
- multimap key_comp() in C++ STL
- multimap find( ) in C++ STL
- multimap value_comp() function in C++ STL
