DATA DICTIONARY - MAPS IN C++
Maps in C++ are collections of data that store data whose values are accessed using a unique key. It is actually a data dictionary and is declared as follows:
map<key_type, value_type> map_name;
map<key_type, value_type> map_name;
Consider the following problem:
Assign the names of groups of students in one class. Enter their average grades at the end of the school year. determine the average of the group and determine how many students achieved above average
Solution: we will solve this example using maps, by using the student's name as the key, and placing their success as a value inside the map.
In order to use maps in C++ inside the file, the map header file must be included:
Solution: we will solve this example using maps, by using the student's name as the key, and placing their success as a value inside the map.
In order to use maps in C++ inside the file, the map header file must be included:
#include < map >
Then you need to declare the map:
map < string, double> average;
We can see that the key types will be string and this refers to student names, while the value types will be double, since the values are actually average grades, so real numbers.
Map initialization in c++
One of the ways to fill the map with values is to initialize it, and this is the case when we know in advance the data about the students, their names and achievements, i.e. their average grades at the end of the year.
average={
{"Mickael",4.52},
{"Milica",4.78},
{"Nikola",3.89 }
};
{"Milica",4.78},
{"Nikola",3.89 }
Iterations through a c++ map
In each cycle of the loop, the next pair containing both the key and the value (Student's Name and average) is taken. The key is accessed using the first attribute, while the value is accessed using the second attribute.
//printing the data stored inside the map
for(auto keyValuePair: averageMap){
for(auto keyValuePair: averageMap){
cout << keyValuePair.first << " " << keyValuePair.second << endl;
}
Image by Peggy und Marco Lachmann-Anke from Pixabay
|
Inserting values into a map in c++
Another way to fill the map with values is to load it from standard input, when the user is asked to enter the name of the student, as well as the student's grade.
Entering a key-value pair can be added to the map at its end with the insert function. Consider the following code:
Entering a key-value pair can be added to the map at its end with the insert function. Consider the following code:
//Loading a key-value pair into a map
int n;
string name;
double average;
map < string,double > averageMap;
cout << " Enter the number of students" << endl;
cin >> n;
for(int i = 0; i < n; i++){
//printing the data stored inside the map
for(auto pairKeyValue: average){
int n;
string name;
double average;
map < string,double > averageMap;
cout << " Enter the number of students" << endl;
cin >> n;
for(int i = 0; i < n; i++){
cout << "Enter the name of the student " << endl;
cin >> name;
cout << "Enter the average " << endl;
cin >> average;
averageMap.insert(pair (name,average));
}cin >> name;
cout << "Enter the average " << endl;
cin >> average;
averageMap.insert(pair
//printing the data stored inside the map
for(auto pairKeyValue: average){
cout << pairKeyValue.first << " " << pairKeyValue.second << endl;
}Rewriting from one folder to another c++
The map averageMap2 is declared, which will have the same elements as averageMap. When declaring, pointers to the beginning and end of the first map should be passed.
//copying from one data dictionary (map) to another map
cout << "Map 2: "<< endl;
map < string, double>averageMap2(averageMap.begin(),averageMap.end());
printing(averageMap2);
cout << "Map 2: "<< endl;
map < string, double>averageMap2(averageMap.begin(),averageMap.end());
printing(averageMap2);
Here, we have moved the printing of map values to a special printing function, to which a pointer to the map to be printed is passed as a parameter.
void printing(map < string, double> averageMap){
//printing the data stored inside the map
for(auto parKeyValue: averageMap){
}for(auto parKeyValue: averageMap){
cout << parKeyValue.first << " " << parKeyValue.second << endl;
}After starting:
Printing values from map using iterator in c++
We define an iterator (cursor) that points to a specific pair within the map and can be easily moved through the map:
map<string, double>::iterator itr;
Writing through the loop would be:
map<string, double>::iterator itr;
Writing through the loop would be:
void printing2(map < string, double> averageMap){
//printing the data stored inside the map
map < string, double>::iterator itr;
for(itr = averageMap.begin(); itr !=averageMap.end(); ++itr ){
}map < string, double>::iterator itr;
for(itr = averageMap.begin(); itr !=averageMap.end(); ++itr ){
cout << itr -> first << " " << itr -> second << endl;
}Deleting elements from the map c++
Deleting elements from the map can be done with the erase function
Deleting elements from the map with a certain key value in c++
If we want to delete an element whose key is, for example, the value nameForDelete, it can be done as follows:
//Deleting value from the map with a certain key
string nameForDelete = "Nikola";
averageMap2.erase(nameForDelete);
cout << "After deletion, map 2 will be: " << endl;
printing2(averageMap2);
string nameForDelete = "Nikola";
averageMap2.erase(nameForDelete);
cout << "After deletion, map 2 will be: " << endl;
printing2(averageMap2);
Delete all elements in the map
//Delete elements of all elements from the map
averageMap2.clear();
cout << "After deletion, with the clear command, the number of elements in averageMap2 is " << averageMap2.size();
averageMap2.clear();
cout << "After deletion, with the clear command, the number of elements in averageMap2 is " << averageMap2.size();
After deleting averageMap2 will not have a single element, i.e. map size is zero.
Finding values inside a map by key in c++
If the key is known, first the find function returns an iterator that points to the pair whose key was passed to the function. The iterator can then be used to retrieve the value, as shown in the following code:
//Searching map
string key="Michael";
map::iterator it;
it=averageMap.find(key);
cout << endl << it -> first << " have an average:" << it->second;
string key="Michael";
map
it=averageMap.find(key);
cout << endl << it -> first << " have an average:" << it->second;
Sorting map by value in c++
First you need to transfer the elements to a vector:
// Declaring a vector of pairs from a map
vector < pair < string, double > > A;
// Copy key-value pair from Map
// to vector of pairs
for (auto& it : averageMap) {
vector < pair < string, double > > A;
// Copy key-value pair from Map
// to vector of pairs
for (auto& it : averageMap) {
A.push_back(it);
}Then sorting is done with the sort function>
sort(A.begin(),A.end());
cout << " After sorting, the map will be: " << endl;
printing(averageMap);
cout << " After sorting, the map will be: " << endl;
printing(averageMap);
After executions:
EXAMPLE USING MAPS: See example number 11 from the website: Qualifications for district competitions
Previous
|< Two dimensional array-matrix |