C++ Map
C++ Map
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(https://srv.carbonads.net/ads/click/x/GTND427ECESIT2JWC674YKQUCT
ADS VIA CARBON
(HTTP://CARBONADS.NET/?UTM_SOURCE=WWWPROGRAMIZCOM&UTM_MEDIUM=AD_VIA_LINK&UTM_CAMPAIGN=IN_UNIT&UTM_TERM=CARBON)
C++ Map
In C++, maps are associative containers that store paired data. These paired
data are called key-value pairs, where the key is unique but the value is not.
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
In order to use maps in C++, we must include the map header file in our program:
#include <map>
Create
Thank a Map
you for printing our content at www.domain-name.com. Please check back soon for new contents.
WeC++
can declare a PRO!
mapClaim
using the following (https://programiz.pro/learn/master-cpp?utm_source=sticky-
syntax:
36% off Try hands-on with Programiz Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials
std::map<key_type, & examples
value_type> map_name = {{key1, value1},{key2, value2}, ...};
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Here,
Note: We can also initialize map elements without using the assignment
operator = . For example,
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
std::map<int, string> student {{1,"Jacqueline"}, {2,"Blake"}, {3,"Denise"}
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Map Methods
In C++, the map class provides various methods to perform different operations
on a map.
Operation Description
WeC++
can use the []PRO!
operator to add (https://programiz.pro/learn/master-cpp?utm_source=sticky-
key-valuepairs to a map. For example,
36% off Try hands-on with Programiz Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
create Search
//(/)
PRO tutorials & examples
a floating&utm_campaign=programiz&utm_medium=referral)
map with integer keys and string values
std::map<int, string> student;
www.domain-name.com
We can also use the insert() function alongside the make_pair() function to
insert elements into the map. For example,
student.insert(std::make_pair(3, "Denise"));
We can generalize the above two methods into the following syntaxes:
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
#include <iostream> banner&utm_campaign=programiz&utm_medium=referral)
#include
Try <map>
Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
Search
PRO namespace tutorials & examples
using
(/) floating&utm_campaign=programiz&utm_medium=referral)
std;
www.domain-name.com
int main() {
return 0;
}
Output
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
Student[1]: Jacqueline
Student[2]: Blake
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-onStudent[3]: Denise
C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Student[4]: Blake
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
Student[5]: Aaron
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
In this program, we have created a map named student that stores int keys
and string values.
Notice that we have two duplicate values ( "Blake" ) in our program, which is
valid.
However, we also have two duplicate keys (5) in our program, which is invalid.
Here, since both "Timothy" and "Aaron" share the same key, only one of the
values is allowed to be stored.
First, "Timothy" is paired with the key 5 in student . Then, when "Aaron" is paired
with 5, "Timothy" is overwritten.
Note: When we use cout to print a map element directly, we only print the
corresponding map value, not its key.
// Output: Jacqueline
cout << student[1];
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
#include <iostream>
#include <map>
using namespace std;
int main() {
student[1] = "Jacqueline";
student[2] = "Blake";
student[3] = "Denise";
student[4] = "Aaron";
return 0;
}
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on1C++
- with Programiz PRO! Claim Discount Now
Jacqueline banner&utm_campaign=programiz&utm_medium=referral)
2 - Blake
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
3 - DeniseSearch tutorials & examples
PRO floating&utm_campaign=programiz&utm_medium=referral)
4 (/)
- Aaron
www.domain-name.com
In the above example, we have used a custom iterator iter to access the keys
and values of the student map. The key is given by the first object, and the
value by the second object.
Notice that we have used a for loop with the iter iterator to display all the
elements of student .
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
Here, banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
begin()
PRO Search tutorials & examples
- floating&utm_campaign=programiz&utm_medium=referral)
returns an iterator that points to the first element of the map
(/)
www.domain-name.com
end() - returns an iterator that points to the theoretical element following the
last element of the map.
We have used the -> operator with iter to access the first and second
objects.
Alternatively, we can also dereference iter and use the . operator to access
first and second .
// Output: 1
cout << (*iter).first << " - ";
// Output: Jacqueline
cout << (*iter).second;
map_name.find(key);
Foryou
Thank example,
for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-onmap<int,
C++ with Programiz
string>PRO! Claim Discount Now
student; banner&utm_campaign=programiz&utm_medium=referral)
map<int, string>::iterator iter;
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
student[1] = "Jacqueline";
student[2] = "Blake"; www.domain-name.com
In the example above, we have used the find() function to search for the
element of student that contains the key 2.
an iterator pointing to the end of the map i.e. student.end() if the element
doesn't exist.
student.clear();
We can use the erase() function to delete individual elements either with the
help of iterators or with keys.
map <string, int> student_rank {{"Josh", 2}, {"Rachel", 4}, {"Betty", 6}};
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
#include <iostream> banner&utm_campaign=programiz&utm_medium=referral)
#include
Try <map>
Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
using
PRO Search
namespace tutorials & examples
std;
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
int main() {
Output
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
Initial Values:
Denise, Blake, Courtney, John, Jennifer,
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Final Values:
Blake,
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
Courtney, Jennifer,
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
In the above program, we have used the erase() function to delete individual
map elements.
First, we deleted the first map element by supplying the itr iterator to erase() .
itr = student.begin();
student.erase(itr);
Note: We have also used the itr iterator to loop through the map.
Then, we deleted the map element possessing the key 4 by supplying the key to
erase() .
student.erase(4);
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
The erase() function can also be used to remove a range of elements from the
map. For this, we need to pass two iterators to the erase() function:
my_map.erase(itr_first, itr_last);
Here,
Note that the elements in a map are internally sorted by their key.
Thank you for printing our content at www.domain-name.com. Please check back soon for new contents.
36% off
Example 3: erase() to Remove a Range of(https://programiz.pro/learn/master-cpp?utm_source=sticky-
Elements
Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
#include
PRO Search tutorials & examples
<iostream>
(/) floating&utm_campaign=programiz&utm_medium=referral)
#include <map>
www.domain-name.com
using namespace std;
int main() {
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-onInitial
C++ with Programiz
Values: PRO! Claim Discount Now banner&utm_campaign=programiz&utm_medium=referral)
Denise, Blake, Courtney, John, Jennifer,
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
Final Values:
Denise, Jennifer, www.domain-name.com
In the above program, we have removed the map elements between the keys 2
and 5.
However, notice that the element with the key 5 is not removed, while the element
with the key 2 is removed.
This is because erase() removes the starting element of the given range but
doesn't remove the end element.
Share on:
(https://www.facebook.com/sharer/sharer.php?
Thank you for printing our content at www.domain-name.com. Please(https://twitter.com/intent/tweet?
check back soon for new contents.
u=https://www.programiz.com/cpp- text=Check%20this%20amazing%20article%20on%
programming/map) programming/map)
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-on C++ with Programiz PRO! Claim Discount Now
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
DidPRO Search
you find this tutorials
article helpful?& examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
Related Tutorials
ThankC++
you for printing our content at www.domain-name.com.
Tutorial Please check back soon for new contents.
C++ Tutorial
(https://programiz.pro/learn/master-cpp?utm_source=sticky-
36% off Try hands-onC++
C++ Multimap
with Programiz PRO! Claim Discount Now C++ Unordered Map
banner&utm_campaign=programiz&utm_medium=referral)
Try Programiz (https://programiz.pro/learn/master-cpp?utm_source=nav-
PRO Search tutorials & examples
(/) floating&utm_campaign=programiz&utm_medium=referral)
www.domain-name.com
(/cpp-programming/multimap) (/cpp-programming/unordered-map)
(/cpp-programming/vectors) (/cpp-programming/pass-return-object-
function)