Contents
Map
The STL has a class called "pair" that can hold 2 elements of different types. It had public data members "first" and "second" .File: pair1.cpp
#include <iostream> #include <string> #include <utility> // Required for pair and make_pair using namespace std ; int main() { // Declaring and initializing a pair pair<string, string> author("Scott", "Meyers") ; // Accessing elements cout << "Author's first name: " << author.first << endl; cout << "Author's last name: " << author.second << endl; // Modifying elements author.first = "Herbert" ; author.second = "Schildt" ; cout << "Author's first name: " << author.first << endl; cout << "Author's last name: " << author.second << endl; // Using make_pair pair<string, int > student = make_pair( "John", 22 ) ; cout << "Name: " << student.first << endl; cout << "Age: " << student.second << endl; return 0; }The "map" class holds two values; a key and a value. The following program shows how it can be used. The map uses the pair class to hold the 2 elements.
File: map1.cpp
// C++ program to illustrate the begin and end iterator #include <iostream> #include <map> #include <string> using namespace std; int main() { // Create a map of strings to integers map<string, int> mapObject ; // Insert some values into the map mapObject["one"] = 1 ; mapObject["two"] = 2 ; mapObject["three"] = 3 ; // Get an iterator pointing to the first element in the // map map<string, int>::iterator it = mapObject.begin() ; // Iterate through the map and print the elements while ( it != mapObject.end() ) { cout << "Key: " << it->first << ", Value: " << it->second << endl; ++it; } cout << "Range loop to access map objects." << endl ; //Range loop for( pair<string, int> p1 : mapObject ) { cout << "Key: " << p1.first << ", Value: " << p1.second << endl; } //for return 0; } $ g++ map1.cpp ; ./a.exe Key: one, Value: 1 Key: three, Value: 3 Key: two, Value: 2 Range loop to access map objects. Key: one, Value: 1 Key: three, Value: 3 Key: two, Value: 2The below program shows some operations on maps.
File: map2.cpp
#include <iostream> #include <iterator> #include <map> using namespace std; int main() { // empty map container map<int, int> mapObject1 ; // insert elements in random order mapObject1.insert( pair<int, int>(1, 40) ); mapObject1.insert( pair<int, int>(2, 30) ); mapObject1.insert( pair<int, int>(3, 60) ); mapObject1.insert( pair<int, int>(4, 20) ); mapObject1.insert( pair<int, int>(5, 50) ); mapObject1.insert( pair<int, int>(6, 50) ); // another way of inserting a value in a map mapObject1[7] = 10; // printing map mapObject1 map<int, int>::iterator itr; cout << "\nThe map mapObject1 is : \n"; cout << "\tKEY\tELEMENT\n"; for (itr = mapObject1.begin(); itr != mapObject1.end(); ++itr) { cout << '\t' << itr->first << '\t' << itr->second << '\n'; } cout << endl; // assigning the elements from mapObject to mapObject2 map<int, int> mapObject2(mapObject1.begin(), mapObject1.end()); // print all elements of the map mapObject2 cout << "\nThe map mapObject2 after" << " assign from mapObject is : \n"; cout << "\tKEY\tELEMENT\n"; for (itr = mapObject2.begin(); itr != mapObject2.end(); ++itr) { cout << '\t' << itr->first << '\t' << itr->second << '\n'; } cout << endl; // remove all elements up to // element with key=3 in mapObject2 cout << "\nmapObject2 after removal of" " elements less than key=3 : \n"; cout << "\tKEY\tELEMENT\n"; mapObject2.erase(mapObject2.begin(), mapObject2.find(3)); for (itr = mapObject2.begin(); itr != mapObject2.end(); ++itr) { cout << '\t' << itr->first << '\t' << itr->second << '\n'; } // remove all elements with key = 4 int num; num = mapObject2.erase(4); cout << "\nmapObject2.erase(4) : "; cout << num << " removed \n"; cout << "\tKEY\tELEMENT\n"; for (itr = mapObject2.begin(); itr != mapObject2.end(); ++itr) { cout << '\t' << itr->first << '\t' << itr->second << '\n'; } cout << endl; // lower bound and upper bound for map mapObject key = 5 //lower_bound first key that is not less than 5 //upper_bound first key that is greater than 5 cout << "mapObject.lower_bound(5) : " << "\tKEY = "; cout << mapObject1.lower_bound(5)->first << '\t'; cout << "\tELEMENT = " << mapObject1.lower_bound(5)->second << endl; cout << "mapObject.upper_bound(5) : " << "\tKEY = "; cout << mapObject1.upper_bound(5)->first << '\t'; cout << "\tELEMENT = " << mapObject1.upper_bound(5)->second << endl; return 0; }
Exercise
1)File: map_ex1.cpp
#include <iostream> #include <map> #include <unordered_map> using namespace std ; void printFreq( string inputString ) { map<char,int> mapObject1 ; for( char ch1 : inputString ) { if ( ch1 == ' ' ) continue ; if ( mapObject1.count( ch1 ) > 0 ) mapObject1[ch1] = mapObject1[ch1] + 1 ; else mapObject1[ch1] = 1 ; } for( pair<char,int> p1 : mapObject1 ) cout << "( " << p1.first << " " << p1.second << " )" ; cout << endl ; } int main () { printFreq( "aabbcb" ) ; printFreq( "George Chuvalo" ) ; return 0; }
File: map_ex1s.cpp