Maps (aka Associative Arrays)

This page is under construction. Please come back later.
#undef NDEBUG
#include "Utils.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main(int argc, char **argv) {
unordered_map<string, string> dictionary = {};
cout << "dictionary size: " << dictionary.size() << endl;
cout << "dictionary is " << (dictionary.empty() ? "empty" : "not empty") << endl;
dictionary["apple"] = "red";
dictionary["banana"] = "yellow";
dictionary["cantaloupe"] = "orange";
dictionary["dragonfruit"] = "red";
dictionary["elderberry"] = "purple";
cout << "dictionary size: " << dictionary.size() << endl;
cout << "dictionary is " << (dictionary.empty() ? "empty" : "not empty") << endl;
cout << "bananas are " << dictionary.at("banana") << endl;
string x = "dragonfruit";
if (dictionary.count(x)) {
cout << x << " are " << dictionary.at(x) << endl;
} else {
cout << "Can't find " << x << endl;
}
x = "fig";
if (dictionary.count(x)) {
cout << x << " are " << dictionary.at(x) << endl;
} else {
cout << "Can't find " << x << endl;
}
x = "elderberry";
dictionary.erase(x);
if (dictionary.count(x)) {
cout << x << " are " << dictionary.at(x) << endl;
} else {
cout << "Can't find " << x << endl;
}
cout << Utils::to_string(dictionary) << endl;
return 0;
}
Output
$ g++ -std=c++17 Maps1.cpp -o Maps1 -lfmt
$ ./Maps1
dictionary size: 0
dictionary is empty
dictionary size: 5
dictionary is not empty
bananas are yellow
dragonfruit are red
Can't find fig
Can't find elderberry
{"dragonfruit" => "red", "cantaloupe" => "orange", "banana" => "yellow", "apple" => "red"}
#undef NDEBUG
#include "Utils.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
using namespace Utils;
using namespace std;
int main(int argc, char **argv) {
unordered_map<string, int> planet_diameters = {
{"Mercury", 4879},
{"Venus", 12103},
{"Earth", 12756},
{"Mars", 6794},
{"Jupiter", 142985},
{"Saturn", 120534},
{"Uranus", 51115},
{"Neptune", 49534},
{"Pluto", 2374},
{"Ceres", 946},
{"Eris", 2326},
{"Makemake", 1430}
};
for (auto planet : Utils::keys(planet_diameters)) {
cout << planet << " has a diameter of " << planet_diameters.at(planet) << " km" << endl;
}
return 0;
}
Output
$ g++ -std=c++17 Maps2.cpp -o Maps2 -lfmt
$ ./Maps2
Ceres has a diameter of 946 km
Earth has a diameter of 12756 km
Eris has a diameter of 2326 km
Jupiter has a diameter of 142985 km
Makemake has a diameter of 1430 km
Mars has a diameter of 6794 km
Mercury has a diameter of 4879 km
Neptune has a diameter of 49534 km
Pluto has a diameter of 2374 km
Saturn has a diameter of 120534 km
Uranus has a diameter of 51115 km
Venus has a diameter of 12103 km
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
-
[[C++ Programming Language]], 4th Edition, Bjarne Stroustrup, Addison-Wesley, 2013, ISBN 978-0321563842.
- [[C++ Language Reference]]
- [[cplusplus.com]]
- [[Cprogramming.com]]
Pure Programmer


