The C++ multimap insert function is utilized to add an element or a set of elements into a multimap data structure.
Syntax
single element (1) pair<iterator,bool> insert (const value_type& val); //until C++ 11
hint (2) iterator insert (iterator position, const value_type& val); //until C++ 11
range (3) template <class InputIterator>
void insert (InputIterator first, InputIterator last); //until C++ 11
single element (1) pair<iterator,bool> insert (const value_type& val);
template <class P> pair<iterator,bool> insert (P&& val); //since C++ 11
hint (2) iterator insert (const_iterator position, const value_type& val);
template <class P> iterator insert (const_iterator position, P&& val);
range (3) template <class InputIterator>
void insert (InputIterator first, InputIterator last); //since C++ 11
initializer list (4) void insert (initializer_list<value_type> il); //since C++ 11
Parameter
val : Key value to insert in the multimap.
Hint for the placement of the element within the structure.
first : Beginning of range to insert.
last : End of range to insert.
il : An initializer list.
Return value
If a singular element is added, it will yield an iterator pointing to the location where the new element was inserted within the multimap.
Or when the element is added with a clue, it yields an iterator indicating the location where the new element was added to the multimap.
Complexity
- If a single element is inserted complexity will be logarithmic in size.
- If a hint is given and the position given is the optimal then the complexity will be amortized constant.
Iterator validity
No changes.
Data Races
The container is modified.
Exception safety
This function does not throw exception.
Example 1
Let's explore a straightforward example of inserting elements into the multimap:
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<char, int> m = {
{'a', 1},
{'b', 2},
{'c', 3},
};
// inserting new element
m.insert(pair<char, int>('b', 4));
m.insert(pair<char, int>('c', 5));
cout << "Multimap contains following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
Output:
Multimap contains following elements
a = 1
b = 2
b = 3
c = 4
c = 5
In the aforementioned example, it merely inserts the element containing the specified key-value pair.
Example 2
Let's examine a straightforward illustration of inserting the element at the designated location:
#include <iostream>
#include <map>
using namespace std;
int main(void) {
multimap<char, int> m = {
{'b', 2},
{'c', 3},
{'d', 4},
};
//inserting element with the given position
m.insert(m.begin(), pair<char, int>('a', 1));
m.insert(m.end(), pair<char, int>('d', 5));
cout << "Multimap contains following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
Output:
Multimap contains following elements
a = 1
b = 2
c = 3
d = 4
d = 5
In the previously mentioned scenario, elements are added at the specified positions, such as inserting {'a', 1} at the beginning and {'d', 5} at the end.
Example 3
Let's explore a straightforward illustration of transferring the elements from one multimap to another:
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<char, int> m1 = {
{'a', 1},
{'b', 2},
{'a', 3},
{'d', 4},
{'b', 5},
};
multimap<char, int> m2; // creating new multimap m2
m2.insert(m1.begin(), m1.end()); //inserting the elements of m1 to m2 from begin to end
cout << "Multimap contains following elements" << endl;
for (auto it = m2.begin(); it != m2.end(); ++it)
cout << it->first << " = " << it->second << endl;
return 0;
}
Output:
Multimap contains following elements
a = 1
a = 3
b = 2
b = 5
d = 4
In the provided scenario, the multimap m1 contains five elements while multimap m2 is currently devoid of any elements. The insert function is being employed to transfer the elements from m1 to m2, starting from the beginning of m1 to its end. Subsequently, the contents of the m2 multimap are being showcased.
Example 4
Let's see a simple example to insert the element:
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<int , string> m = {
{1, "Java"},
{2, "C++"},
{3, "SQL"},
};
m.insert({{4,"VB"}, {3, "Oracle"}});
cout << "Multimap contains following elements" << endl;
for (auto it = m.begin(); it != m.end(); ++it)
cout << it->first << " : " << it->second << endl;
return 0;
}
Output:
Multimap contains following elements
1 : Java
2 : C++
3 : SQL
3 : Oracle
4 : VB
In the given scenario, a different variation of the insert method is employed to add the elements to the multimap.