File tree 3 files changed +57
-10
lines changed 3 files changed +57
-10
lines changed Original file line number Diff line number Diff line change 4
4
// Stores element in sorted order
5
5
6
6
#include < iostream>
7
- #include < set>
7
+ #include < set>
8
8
9
9
using namespace std ;
10
10
11
- int main (){
11
+ int main ()
12
+ {
12
13
// declaration
13
14
set<int > s;
14
15
@@ -20,24 +21,24 @@ int main(){
20
21
s.insert (5 );
21
22
22
23
// printing the set
23
- set<int , greater<int > >::iterator itr;
24
+ set<int , greater<int >>::iterator itr;
24
25
for (itr = s.begin (); itr != s.end (); itr++)
25
26
{
26
- cout <<*itr<< " " ;
27
+ cout << *itr << " " ;
27
28
}
28
- cout<< endl;
29
+ cout << endl;
29
30
30
31
// finding any elements require O(log n)
31
32
// "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;
34
35
// "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;
36
37
37
38
// deleting all elements in the set
38
- cout<< " Is the set empty? " << s.empty ()<< endl;
39
+ cout << " Is the set empty? " << s.empty () << endl;
39
40
s.clear ();
40
- cout<< " Is the set empty now? " << s.empty ()<< endl;
41
+ cout << " Is the set empty now? " << s.empty () << endl;
41
42
42
43
return 0 ;
43
44
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments