In this instructional exercise you will find out about STL Map holder in C++ i.e., std:: map and all capacities material on it.
The guide is an acquainted holder. Guide fulfils “acquainted”. That implies each an incentive in the map is related to a key.
All keys are novel. No two mapped qualities can have the same key. The sort of key and put away qualities may contrast. All components pursue a severe request.
At the point when we embed component naturally put away in its right position.
C++ STL Map
Iterators that can be pertinent on map:
start(): returns iterator to the start.
end(): returns iterator as far as possible of the guide.
rbegin(): returns turn around iterator to invert starting.
rip(): returns switch iterator to turn around end.
cbegin(): Returns consistent iterator to the start.
cend(): Returns steady iterator as far as possible.
These iterators we can use in our projects.
The first thing we have to incorporate the map header record. Which is #include
Let see a few capacities related to the map:
Embeddings component into the map:
At whatever point a component embedded into the map, size of the guide increments. Since the map contains special keys when we attempt to embed a component into the map,
it generally checks whether any component previously embedded or not with this equivalent key worth.
There are various ways we can embed components into the map.
Strategy 1: Insert legitimately bypassing component and its comparing key.
map name.insert (pair ( key, esteem ));
Strategy 2: Using iterator. This profits iterator in the embedded position.
map name.insert (iterator, pair < keyDatatype, esteem datatype > (key worth, information esteem));
A model program for embeddings into the map:
#include <iostream>
#include <map>
#include <iterator>
using namespace std;
int main(){
map < int, int> mp1; // declaring map
map < int, int> :: iterator it;
// Inserting method 1:
for (int i=0; i<5; i++) {
mp1.insert (pair < int, int> (i, i*10));
}
// Caution: If you give any constant in place of first argument in insert. Only the first value will be inserted. Remaining will be pushed out as duplicates.
// inserting method 2:
it= mp1.begin();
mp1.insert(it, pair < int, int> (99, 99));
// Even this was inserted at starting position, due to porperty of ordering of associative containers it sorted according to its key value and inserted at last position.
it= mp1.end();
mp1.insert(it, pair < int, int> (10, 10));
// printing
for ( it= mp1.begin(); it!=mp1.end(); it++) {
cout << it -> first;
cout << '\t';
cout << it -> second;
cout << endl;
}
return 0;
}
Output
0 0
1 10
2 20
3 30
4 40
10 10
99 99
Other adjustment works on map:
eradicate(): We can evacuate a single component or scope of components. The impact on the map is at whatever point a worth erased from the map, its size gets diminished.
swap(): swaps components of map1 to map2 and map2 to map1. Here both map1 and map2 are needed not to be of the same size.
clear(): evacuates all components in the guide. It results in a guide of size 0.
Model program to appear above work:
#include <iostream>
#include <map>
#include <iterator>
using namespace std;
int main(){
map < int, int > mp1;
map < int, int > mp2;
map <int, int> :: iterator it;
for (int i=0; i<5; i++){
// inserting elements into map
mp1.insert (pair <int, int> (i,i+10));
}
// deleting element pointed by iterator
it = mp1.begin();
mp1.erase(it); // key 0 deleted
cout << "printing remaining elements after one erase operation" << endl;
for ( it= mp1.begin(); it!=mp1.end(); it++) {
cout << it -> first;
cout << '\t';
cout << it -> second;
cout << endl;
}
// erasing range of values
it= mp1.begin();
mp1.erase(it,mp1.end());
// this will erase all map
cout << "checking map empty or not" << endl;
int chk= mp1.empty();
if (chk==1 )
cout << "map is empty" << endl;
else
cout << "map is not empty" << endl;
for (int i=0; i<5; i++){
// inserting elements into map1
mp1.insert (pair <int, int> (i,i+10));
}
for (int i=0; i<5; i++){
// inserting elements into map2
mp2.insert (pair <int, int> (i,i*10));
}
cout << "map1 elements before swap" << endl;
for ( it= mp1.begin(); it!=mp1.end(); it++) {
cout << it -> first;
cout << '\t';
cout << it -> second;
cout << endl;
}
cout << "map2 elements before swap" << endl;
for ( it= mp2.begin(); it!=mp2.end(); it++) {
cout << it -> first;
cout << '\t';
cout << it -> second;
cout << endl;
}
// doing swaping operation
mp1.swap(mp2);
cout << "map1 elements after swap" << endl;
for ( it= mp1.begin(); it!=mp1.end(); it++) {
cout << it -> first;
cout << '\t';
cout << it -> second;
cout << endl;
}
cout << "map2 elements after swap" << endl;
for ( it= mp2.begin(); it!=mp2.end(); it++) {
cout << it -> first;
cout << '\t';
cout << it -> second;
cout << endl;
}
cout << "applying clear operation" << endl;
mp1.clear(); mp2.clear();
chk= mp1.empty();
if (chk == 1)
cout << "map is empty by clear operation" << endl;
else
cout << "map is not empty";
return 0;
}
Output
printing remaining elements after one erase operation
1 11
2 12
3 13
4 14
checking map empty or not
map is empty
map1 elements before swap
0 10
1 11
2 12
3 13
4 14
map2 elements before swap
0 0
1 10
2 20
3 30
4 40
map1 elements after swap
0 0
1 10
2 20
3 30
4 40
map2 elements after swap
0 10
1 11
2 12
3 13
4 14
applying clear operation
map is empty by clear operation
Capacities identified with limit:
void(): restores a Boolean worth whether the guide is vacant or not.
size(): restores the size of the guide.
max_size(): restores the most extreme size a guide can have.
Model program to appear above capacities:
#include <iostream>
#include <map>
#include <iterator>
using namespace std;
int main(){
map < int, int> mp1;
map < int, int> :: iterator it;
for(int i=0; i<5; i++) {
mp1.insert (pair <int, int> (i, i*10 ));
}
int chk = mp1.empty();
if (chk == 1)
cout << " map is empty" << endl;
else
cout << "map contains some elements" << endl;
cout << "size of the map is " ;
cout << mp1.size() << endl;
cout << "maximum size of the map is " ;
cout << mp1.max_size() << endl;
return 0;
}
Output
map contains some elements
size of the map is 5
maximum size of the map is 461168601842738790
Remark beneath on the off chance that you have questions or discovered any data erroneous in above instructional exercise for C++ STL Map holder, for example, std:: map.