Skip to content

Commit 27a114a

Browse files
committed
unordered_map
1 parent faf5c41 commit 27a114a

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

algorithm.cpp

Whitespace-only changes.

set.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
// Stores element in sorted order
55

66
#include <iostream>
7-
#include<set>
7+
#include <set>
88

99
using namespace std;
1010

11-
int main(){
11+
int main()
12+
{
1213
// declaration
1314
set<int> s;
1415

@@ -20,24 +21,24 @@ int main(){
2021
s.insert(5);
2122

2223
// printing the set
23-
set<int, greater<int> >::iterator itr;
24+
set<int, greater<int>>::iterator itr;
2425
for (itr = s.begin(); itr != s.end(); itr++)
2526
{
26-
cout <<*itr<<" ";
27+
cout << *itr << " ";
2728
}
28-
cout<<endl;
29+
cout << endl;
2930

3031
// finding any elements require O(log n)
3132
// "find" function returns the iterator in O(log n)
32-
cout<<"Print 5 if present ";
33-
cout<<*s.find(5)<<endl;
33+
cout << "Print 5 if present ";
34+
cout << *s.find(5) << endl;
3435
// "count" function returns bool value in O(log n) time
35-
cout<<"Is 9 present in the set? "<<s.count(9)<<endl;
36+
cout << "Is 9 present in the set? " << s.count(9) << endl;
3637

3738
// deleting all elements in the set
38-
cout<<"Is the set empty? "<<s.empty()<<endl;
39+
cout << "Is the set empty? " << s.empty() << endl;
3940
s.clear();
40-
cout<<"Is the set empty now? "<<s.empty()<<endl;
41+
cout << "Is the set empty now? " << s.empty() << endl;
4142

4243
return 0;
4344
}

unordered_map.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// stores in key-value pair
2+
// all kyes are not unique
3+
// not stored in sorted order
4+
5+
#include <iostream>
6+
#include <unordered_map>
7+
8+
using namespace std;
9+
10+
int main(){
11+
// declaration
12+
unordered_map<int,string> um;
13+
14+
// assigning values
15+
// m[key]=value
16+
um[1]="Adarsh";
17+
um[2]="Navneet";
18+
um[30]="Sinha";
19+
20+
// inserting elements using insert function
21+
um.insert({5,"geeky01adarsh"});
22+
23+
// printing the map
24+
for(auto i:um){
25+
cout<<"key = "<<i.first<<" and its value = "<<i.second<<endl;
26+
}
27+
cout<<endl;
28+
29+
// to check if an element is present or not(time-- O(1))
30+
cout<<"Is 13 present in map? "<<um.count(13)<<endl;
31+
32+
// to return iterator where any element is present(time-- O(1))
33+
um.find(1);
34+
35+
// deleting any key-value pair
36+
// takes key name as argument
37+
um.erase(2);
38+
// checking the map
39+
for(auto i:um){
40+
cout<<"key = "<<i.first<<" and its value = "<<i.second<<endl;
41+
}
42+
cout<<endl;
43+
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)