#include <iostream>
#include <map>
int main() {
std::map<int, int> myMap;
myMap[4] = 40;
myMap[1] = 10;
myMap[6] = 60;
myMap[3] = 30;
myMap[2] = 20;
myMap[5] = 50;
std::cout << "Original order:" << std::endl;
for (const auto& pair : myMap) {
std::cout << pair.first << " => " << pair.second << std::endl;
}
std::map<int, int> reorderedMap;
for (const auto& pair : myMap) {
reorderedMap[pair.first * 2] = pair.second;
}
std::cout << "\nReordered map by multiplying keys by 2:" << std::endl;
for (const auto& pair : reorderedMap) {
std::cout << pair.first << " => " << pair.second << std::endl;
}
return 0;
}
- Included necessary header files.
- Defined the
main()
function. - Created an empty
std::map<int, int>
called myMap
. - Added key-value pairs to
myMap
in arbitrary order: (4, 40), (1, 10), (6, 60), (3, 30), (2, 20), (5, 50). - Printed the original order of
myMap
. - Created an empty
std::map<int, int>
named reorderedMap
. - Iterated through
myMap
and multiplied each key by 2 and stored the result along with the corresponding value in reorderedMap
. - Printed the contents of
reorderedMap
after reordering the keys by multiplying them by 2. - Terminated the program.