
- 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
map insert() in C++ STL
In this article we will be discussing the working, syntax and examples of map::insert() function in C++ STL.
What is a Map in C++ STL?
Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.
What is a map::insert()?
map::insert() function is an inbuilt function in C++ STL, which is defined in
As the keys in the map container are unique, the insertion operations check whether each element which is to be inserted has a key which is already existing in the container, if yes then the element is not inserted.
Also, the map container maintains all their elements via their respective key in the ascending order. So, whenever we insert an element it goes to its respective position according to its key.
Syntax
1. Map_name.insert({key& k, value_type& val}); or 2. Map_name.insert(iterator& it, {key& k, value_type& val}); or 3. Map_name.insert(iterator& position1, iterator& position2);
Parameter
This function accepts the following parameter
k − This is the key which 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.
Return value
This function returns an iterator to the element newly inserted in the map container.
Example
Input
map<char, int> newmap; newmap[‘a’] = 1; newmap[‘b’] = 2; newmap[‘c’] = 3; newmap.insert({d, 50});
Output
a:1 b:2 c:3 d:50
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_Map; TP_Map.insert({3, 50}); TP_Map.insert({2, 30}); TP_Map.insert({1, 10}); TP_Map.insert({4, 70}); cout<<"TP Map is : \n"; cout << "MAP_KEY\tMAP_ELEMENT\n"; for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) { cout << i->first << "\t" << i->second << endl; } return 0; }
Output
TP Map is: MAP_KEY MAP_ELEMENT 1 10 2 30 3 50 4 70
Example
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> TP_Map; TP_Map.insert({3, 50}); TP_Map.insert({2, 30}); TP_Map.insert({1, 10}); TP_Map.insert({4, 70}); auto i = TP_Map.find(4); TP_Map.insert(i, { 5, 80 }); cout<<"TP Map is : \n"; cout << "MAP_KEY\tMAP_ELEMENT\n"; for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) { cout << i->first << "\t" << i->second << endl; } return 0; }
Output
TP Map is: MAP_KEY MAP_ELEMENT 1 10 2 30 3 50 4 70 5 80
- Related Articles
- map::at() and map::swap() in C++ STL
- map equal_range() in C++ STL
- map emplace() in C++ STL
- map get_allocator in C++ STL
- map value_comp() in C++ STL
- map::clear() in C++ STL
- map operator= in C++ STL
- map::empty() in C++ STL
- map::size() in C++ STL
- map::at() in C++ STL
- map max_size() in C++ STL
- Set vs Map in C++ STL
- map emplace_hint() function in C++ STL
- map erase() function in C++ STL
- map find() function in C++ STL
