Add cmap_example.cpp

This commit is contained in:
pudepiedj 2023-10-05 11:45:20 +01:00
parent 5a5a71d7bd
commit 1bb192fc27

23
scripts/cmap_example.cpp Normal file
View file

@ -0,0 +1,23 @@
// example of a C/C++ equivalent data structure to the python dict
// there are two: std::map automatically sorts on key; std::unordered_map doesn't
#include <iostream>
#include <map>
int main() {
std::map<std::string, int> dict;
dict["apple"] = 5;
dict["banana"] = 2;
dict["orange"] = 7;
// Accessing elements in the map
std::cout << "Value of apple: " << dict["apple"] << std::endl;
for (const auto& pair : dict) {
std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
}
return 0;
}