
- 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 insert() in C++ STL
In this article we will be discussing the working, syntax, and examples of multimap::insert() function in C++ STL.
What is Multimap in C++ STL?
Multimaps are the associative containers, which are similar to map containers. It also facilitatesto 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::insert()?
multimap::insert() function is an inbuilt function in C++ STL, which is defined in
Unlike a map container that checks the associated key already present then won’t insert the elements but multimap has the feature of associating multiple elements to the same key.
So, whenever we insert an element it goes to its respective position according to its key.
Syntax
multiMap_name.insert({key& k, value_type& val});
or
multiMap_name.insert(iterator& it, {key& k, value_type& val});
or
multiMap_name.insert(iterator& position1, iterator& position2);
or
multimap_name.insert(initializer_list <value_type> il);
Parameter
This function accepts the following parameter −
k − This is the key that is associated with the element. The function checks if the key is already in the container then it doesn’t insert the element.
val − The value which is to be inserted.
it − The iterator type of value which is used to give the position where we want the element to be inserted.
position1, position2 − The position1 is the starting position and position2 is the ending position when we want to insert the series of elements, we can use the range of multiple elements which we want to insert.
il − This is the initializer list containing the elements which we are wishing to initialise to the container.
Return value
This function returns an iterator to the element newly inserted in the map container.
Input
mutlimap<int, char> mymap; mymap.insert(1, ‘a’); mymap.insert(2, ‘b’);
Output
1: a 2: b
Example
//inserting at a given position with a given key
#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 << "Elements 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
Example
//inserting the element after the given key #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({ 5, 40 }); mul.insert({ 6, 50 }); //finding the element after which we will insert auto i = mul.find(3); mul.insert(i, { 4, 90 }); // print the elements cout << "KEY\tELEMENT\n"; for (auto itr = mul.begin(); itr!= mul.end(); ++itr){ cout << itr->first << '\t' << itr->second << '\n'; } return 0; }
Output
If we run the above code it will generate the following output −
KEY ELEMENT 1 10 2 20 3 30 4 90 5 40 6 50
- 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::emplace() in C++ STL
- multimap::emplace_hint() in C++ STL
- multimap::count() in C++ STL
- multimap key_comp() in C++ STL
- multimap value_comp() function in C++ STL
