#include <iostream>
#include <map>
int main() {
// Declare a map that maps strings (names) to integers (ages)
std::map<std::string, int> ageMap;
// 1. Element Access:
ageMap["Alice"] = 25; // Using operator[]
ageMap["Bob"] = 30;
try {
std::cout << "Alice's age: " << ageMap.at("Alice") << std::endl; // Using at()
std::cout << "Bob's age: " << ageMap.at("Bob") << std::endl;
} catch (const std::out_of_range& e) {
std::cout << "Key not found!" << std::endl;
}
// 2. Iterators:
std::cout << "\nIterating using begin() and end():" << std::endl;
for (auto it = ageMap.begin(); it != ageMap.end(); ++it) {
std::cout << it->first << " is " << it->second << " years old." << std::endl;
}
std::cout << "\nIterating in reverse using rbegin() and rend():" << std::endl;
for (auto it = ageMap.rbegin(); it != ageMap.rend(); ++it) {
std::cout << it->first << " is " << it->second << " years old." << std::endl;
}
// 3. Capacity:
std::cout << "\nChecking map size and capacity:" << std::endl;
std::cout << "Is the map empty? " << (ageMap.empty() ? "Yes" : "No") << std::endl;
std::cout << "Map size: " << ageMap.size() << std::endl;
std::cout << "Maximum possible size: " << ageMap.max_size() << std::endl;
// 4. Modifiers:
std::cout << "\nInserting a new element using insert():" << std::endl;
ageMap.insert(std::make_pair("Charlie", 35)); // Using insert
std::cout << "Charlie has been inserted." << std::endl;
std::cout << "\nInserting a new element using emplace():" << std::endl;
ageMap.emplace("David", 40); // Using emplace
std::cout << "David has been emplaced." << std::endl;
std::cout << "\nErasing Bob's entry using erase(key):" << std::endl;
ageMap.erase("Bob"); // Remove Bob
for (const auto& pair : ageMap) {
std::cout << pair.first << " is " << pair.second << " years old." << std::endl;
}
std::cout << "\nClearing all elements using clear():" << std::endl;
ageMap.clear(); // Remove all elements
std::cout << "Map cleared. Size: " << ageMap.size() << std::endl;
// Reinserting data for further operations
ageMap["Alice"] = 25;
ageMap["Charlie"] = 35;
ageMap["David"] = 40;
std::cout << "\nSwapping contents with a new map:" << std::endl;
std::map<std::string, int> newMap;
newMap["Edward"] = 50;
newMap["Frank"] = 55;
ageMap.swap(newMap); // Swap with another map
for (const auto& pair : ageMap) {
std::cout << pair.first << " is " << pair.second << " years old." << std::endl;
}
// 5. Lookup:
std::cout << "\nUsing find() to check for Charlie:" << std::endl;
if (newMap.find("Charlie") != newMap.end()) {
std::cout << "Charlie found!" << std::endl;
} else {
std::cout << "Charlie not found!" << std::endl;
}
std::cout << "\nUsing count() to see if Alice exists:" << std::endl;
std::cout << "Alice count: " << newMap.count("Alice") << std::endl;
std::cout << "\nUsing contains() (C++20) to check for David:" << std::endl;
#if __cplusplus >= 202002L // This works in C++20 and later
std::cout << (newMap.contains("David") ? "David found!" : "David not found!") << std::endl;
#else
std::cout << "David not found!" << std::endl;
#endif
std::cout << "\nUsing lower_bound() and upper_bound() for Charlie:" << std::endl;
auto lower = newMap.lower_bound("Charlie");
auto upper = newMap.upper_bound("Charlie");
if (lower != newMap.end()) {
std::cout << "Lower bound for Charlie: " << lower->first << std::endl;
} else {
std::cout << "Lower bound not found!" << std::endl;
}
if (upper != newMap.end()) {
std::cout << "Upper bound for Charlie: " << upper->first << std::endl;
} else {
std::cout << "Upper bound not found!" << std::endl;
}
std::cout << "\nUsing equal_range() for Charlie:" << std::endl;
auto range = newMap.equal_range("Charlie");
if (range.first != newMap.end()) {
std::cout << "Equal range lower bound: " << range.first->first << std::endl;
}
if (range.second != newMap.end()) {
std::cout << "Equal range upper bound: " << range.second->first << std::endl;
}
// 6. Observers:
std::cout << "\nUsing key_comp() to compare keys:" << std::endl;
std::map<std::string, int>::key_compare kc = newMap.key_comp();
if (kc("Alice", "Bob")) {
std::cout << "Alice is less than Bob" << std::endl;
} else {
std::cout << "Alice is not less than Bob" << std::endl;
}
std::cout << "\nUsing value_comp() to compare values:" << std::endl;
std::map<std::string, int>::value_compare vc = newMap.value_comp();
if (vc(std::make_pair("Alice", 25), std::make_pair("Bob", 30))) {
std::cout << "Alice's age is less than Bob's" << std::endl;
} else {
std::cout << "Alice's age is not less than Bob's" << std::endl;
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: