From 1bb192fc2747f8693a0301cca5c36a09165ac1f9 Mon Sep 17 00:00:00 2001 From: pudepiedj Date: Thu, 5 Oct 2023 11:45:20 +0100 Subject: [PATCH] Add cmap_example.cpp --- scripts/cmap_example.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 scripts/cmap_example.cpp diff --git a/scripts/cmap_example.cpp b/scripts/cmap_example.cpp new file mode 100644 index 000000000..78b633025 --- /dev/null +++ b/scripts/cmap_example.cpp @@ -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 +#include + +int main() { + std::map 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; +} \ No newline at end of file