Directory Listing | |
---|---|
map.cpp
|
|
multimap.cpp
|
|
multiset.cpp
|
|
set.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
30
31
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
map<string, int> obj;
map<string, int>::iterator it;
//We have two ways to insert:
obj.insert(make_pair("hi", 3));
obj.insert(make_pair("dr. gregor", 2));
obj.insert(make_pair("this is a map", 4));
//Iterate through
for (it = obj.begin(); it != obj.end(); it++)
cout << it->first << " -> " << it->second << endl;
obj.clear();
//Insert the second way
obj["hi"] = 3;
obj["dr. gregor"] = 2;
obj["this is a map"] = 4;
//Iterate through
for (it = obj.begin(); it != obj.end(); it++)
cout << it->first << " -> " << it->second << endl;
}