#include #include #include using namespace std; int main() { // empty map container map mapObject1 ; // insert elements in random order mapObject1.insert( pair(1, 40) ); mapObject1.insert( pair(2, 30) ); mapObject1.insert( pair(3, 60) ); mapObject1.insert( pair(4, 20) ); mapObject1.insert( pair(5, 50) ); mapObject1.insert( pair(6, 50) ); // another way of inserting a value in a map mapObject1[7] = 10; // printing map mapObject1 map::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 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; }