-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaps.cpp
More file actions
89 lines (73 loc) · 3.86 KB
/
Copy pathMaps.cpp
File metadata and controls
89 lines (73 loc) · 3.86 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <map>
using namespace std;
// In map mp , the values be will be in sorted order according to the key.
// Note: N is the number of elements in map.
// Some Member Functions of map:
// at( ): Returns a reference to the mapped value of the element identified with key.Its time complexity is O(logN).
// count( ): searches the map for the elements mapped by the given key and returns the number of matches.As map stores each element with unique key, then it will return 1 if match if found otherwise return 0.Its time complexity is O(logN).
// clear( ): clears the map, by removing all the elements from the map and leaving it with its size 0.Its time complexity is O(N).
// begin( ): returns an iterator(explained above) referring to the first element of map.Its time complexity is O(1).
// end( ): returns an iterator referring to the theoretical element(doesn’t point to an element) which follows the last element.Its time complexity is O(1).
// empty( ): checks whether the map is empty or not. It doesn’t modify the map.It returns 1 if the map is empty otherwise returns 0.Its time complexity is O(1).
// erase( ): removes a single element or the range of element from the map.
// find( ): Searches the map for the element with the given key, and returns an iterator to it, if it is present in the map otherwise it returns an iterator to the theoretical element which follows the last element of map.Its time complexity is O(logN).
// insert( ): insert a single element or the range of element in the map.Its time complexity is O(logN), when only element is inserted and O(1) when position is also given.
int main(){
map <char,int> mp;
map <char,int> mymap,mymap1;
//insert elements individually in map with the combination of key value and value of element
mp.insert(pair<char,int>('a',2)); //key is 'c' and 2 is value.
mp.insert(pair<char,int>('b',1));
mp.insert(pair<char,int>('c',43));
//inserts elements in range using insert() function in map 'mymap'.
mymap.insert(mp.begin(),mp.end());
//declaring iterator for map
map <char,int>::iterator it;
//using find() function to return reference of element mapped by key 'b'.
it = mp.find('b');
//prints key and element's value.
cout<<"Key and element's value of map are: ";
cout<<it->first<<" and "<<it->second<<endl;
//alternative way to insert elements by mapping with their keys.
mymap1['x'] = 23;
mymap1['y'] = 21;
cout<<"Printing element mapped by key 'b' using at() function : "<<mp.at('b')<<endl;
//swap contents of 2 maps namely mymap and mymap1.
mymap.swap(mymap1);
/* prints swapped elements of mymap and mymap1 by iterating all the elements through
using iterator. */
cout<<"Swapped elements and their keys of mymap are: "<<endl;
for(it=mymap.begin();it!=mymap.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<"Swapped elements and their keys of mymap1 are: "<<endl;
for(it=mymap1.begin();it!=mymap1.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
//erases element mapped at 'c'.
mymap1.erase('c');
//prints all elements of mymap after erasing element at 'c'.
cout<<"Elements of mymap1 after erasing element at key 'c' : "<<endl;
for(it=mymap1.begin();it!=mymap1.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
//erases elements in range from mymap1
mymap1.erase(mymap1.begin(),mymap1.end());
cout<<"As mymap1 is empty so empty() function will return 1 : " << mymap1.empty()<<endl;
//number of elements with key = 'a' in map mp.
cout<<"Number of elements with key = 'a' in map mp are : "<<mp.count('a')<<endl;
//if mp is empty then itmp.empty will return 1 else 0.
if(mp.empty())
{
cout<<"Map is empty"<<endl;
}
else
{
cout<<"Map is not empty"<<endl;
}
return 0;
}