Skip to content

Commit 601cc4b

Browse files
authored
Create Orderedmap.cpp
1 parent 9d23297 commit 601cc4b

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <map>
4+
using namespace std;
5+
6+
int main() {
7+
8+
// creation of Ordered Map
9+
map<string, int> m;
10+
11+
// insertion
12+
// Approach 1
13+
pair<string, int> p = make_pair("Hello", 1);
14+
m.insert(p);
15+
16+
// Approach 2
17+
pair<string, int> p2("World", 2);
18+
m.insert(p2);
19+
20+
// Approach 3
21+
pair<string, int> p3{"NewWorld", 3};
22+
m.insert(p3);
23+
24+
// Approach 4
25+
m["World"] = 4;
26+
27+
28+
cout << "-------- Searching --------" << endl;
29+
// Searching
30+
// Approach 1
31+
cout << "Value of 'World' Key : "<< m["World"] << endl;
32+
33+
// Approach 2
34+
cout << "Value of 'World' Key : " <<m.at("NewWorld") << endl;
35+
36+
// Search for Un-known key
37+
// cout << m.at("unknown") << endl; // output : error
38+
// cout << m["unknown"] << endl; // output : 0
39+
40+
// But now change the order at see what it returns.
41+
cout << "Value of 'unknown' Key : " << m["unknown"] << endl; // output : 0
42+
cout << "Value of 'unknown' Key : " << m.at("unknown") << endl; // output : 0
43+
44+
// Updation
45+
m["World"] = 5;
46+
47+
cout << "-------- Checking Size --------" << endl;
48+
// Size
49+
cout << "Size of map : " << m.size() << endl;
50+
51+
cout << "-------- Check Presence --------" << endl;
52+
// Check Presence
53+
cout << "Is 'World' Key Value Present or Not : "<< m.count("World") << endl; // true
54+
cout << "Is 'Unknown' Key Value Present or Not : "<< m.count("Unknown") << endl; // false
55+
56+
// Erase
57+
m.erase("World");
58+
cout << "Size of map : " << m.size() << endl;
59+
60+
cout << "-------- For Loop Iteration --------" << endl;
61+
// Iteration
62+
for(auto i : m) cout << "Iterting Key Value Pair : " << i.first << " " << i.second << endl;
63+
64+
cout << "-------- Iterators --------" << endl;
65+
// Iterators
66+
map<string, int> :: iterator it = m.begin();
67+
68+
while(it != m.end()){
69+
cout << "Iterting Key Value Pair : " << it->first << " " << it->second << endl;
70+
it++;
71+
}
72+
return 0;
73+
}

0 commit comments

Comments
 (0)