-
Notifications
You must be signed in to change notification settings - Fork 0
/
015_Container-map.cpp
29 lines (25 loc) · 959 Bytes
/
015_Container-map.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <map>
int main(int argc, char const *argv[]) {
std::map<std::string, int> age;
age["Fred"] = 42; // Fred is 42 years old
age["Barney"] = 37; // Barney is 37 years old
age.insert( std::pair<std::string,int>("pritesh", 25) ); //insert at the end
std::map<std::string,int>::iterator it = age.begin();
age.insert(it, std::pair<std::string,int>("Mark", 40) ); //insert at the begin
age.insert(it, std::pair<std::string,int>("Leo", 69) ); //insert at the begin
if (1){
++ age["Fred"];
}
std::cout << "Fred is " << age["Fred"] << " years old\n";
for (auto& x: age) {
std::cout << x.first << " = " << x.second << '\n';
}
std::cout << "\nUsing complete for loop" << '\n';
for (std::map<std::string,int>::iterator it=age.begin(); it!=age.end(); ++it)
{
std::cout << it->first << " = " << it->second << '\n';
}
return EXIT_SUCCESS;
}