Map Demo

// main.cpp // MapDemo // Created by Kevin Roark on 7/4/23. // Demo of C++ Map containers and functions #include <iostream> #include <map> using namespace std; int main() { map<string, int> age; // Use emplace to insert a new element into the map. // emplace returns a pair. // The first element of the pair is an iterator pointing // to the inserted element. // The second element of the pair is a boolean that is true if the insert took place. auto result = age.emplace("Kevin", 39); if (result.second) { cout << "Kevin was inserted into the map.\n"; } // You can also insert multiple elements at once age.emplace("Bob", 30); age.emplace("Sally", 35); // Use at to access elements // (throws an exception if the key is not found) try { cout << "Kevin's age is " << age.at("Kevin") << ".\n"; } catch (const out_of_range& e) { cout << "Key not found: " << e.what() << "\n"; } // Use count to check if a key is in the map if (age.count("Bob") > 0) { cout << "Bob is in the map.\n"; } else { cout << "Bob is not in the map.\n"; } //now lets iterate through the map to show all cout << "\nAll of the elements in the Map:\n"; for (const auto& pair : age) { std::cout << pair.first << " is " << pair.second << " years old.\n"; } // Use erase to remove an element from the map cout << "\nRemove Bob:\n"; age.erase("Bob"); if (age.count("Bob") == 0) { cout << "Bob was removed from the map.\n"; } //now lets iterate through the map to show all cout << "\nAll of the elements in the Map:\n"; for (const auto& pair : age) { std::cout << pair.first << " is " << pair.second << " years old.\n"; } // Use clear to remove all elements from the map cout << "\nRemove All of the elements in the Map:\n"; age.clear(); if (age.empty()) { cout << "All elements were removed from the map.\n"; } return 0; }

This C++ program demonstrates the use of the std::map container and various associated functions. The std::map named age maps std::string keys (names) to int values (ages).

  1. emplace(): The emplace() function is used to insert elements into the map. It constructs the key-value pair in place, making it more efficient than inserting with the assignment operator. The emplace() function returns a std::pair whose first element is an iterator pointing to the inserted element and whose second element is a boolean indicating whether the insertion took place.

    For example, age.emplace("Kevin", 39) inserts the key-value pair "Kevin"-39 into the map. If the insertion is successful (i.e., the key "Kevin" was not already in the map), the program outputs "Kevin was inserted into the map."

  2. at(): The at() function is used to access the value associated with a key in the map. If the key does not exist, at() throws a std::out_of_range exception. In the program, the at() function is used inside a try-catch block to handle potential exceptions.

    For example, cout << "Kevin's age is " << age.at("Kevin") << ".\\n"; prints "Kevin's age is 39." If "Kevin" is not found in the map, it catches the exception and outputs "Key not found: ..."

  3. count(): The count() function is used to check if a key exists in the map. It returns the number of elements matching a specific key. Since map keys are unique, the return value will either be 0 (if the key is not found) or 1 (if the key is found).

    For instance, if (age.count("Bob") > 0) checks if "Bob" exists in the map and prints a corresponding message.

  4. erase(): The erase() function is used to remove an element from the map by its key. If the key is successfully found and removed, the function returns the number of elements removed (which can be either 0 or 1).

    The statement age.erase("Bob"); removes "Bob" from the map.

  5. clear(): The clear() function is used to remove all elements from the map.

The map's contents are printed to the console using a range-based for loop, and changes to the map are demonstrated by printing its contents before and after the removal operations.

Finally, age.empty() checks whether the map is empty after all its elements have been removed with clear().

2024 - Programming 3 / Data Structures - Author: Dr. Kevin Roark