Add cmap_example.cpp
This commit is contained in:
parent
5a5a71d7bd
commit
1bb192fc27
1 changed files with 23 additions and 0 deletions
23
scripts/cmap_example.cpp
Normal file
23
scripts/cmap_example.cpp
Normal 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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue