This is a header-only C++ template library implementation of a Python dictionary. In particular the ordering of elements within a Python dictionary is determined by the order they are added. The aim of this project is to provide a self-contained structure which provides similar methods to a Python dictionary in C++.
A Dict<Key, Value> is a template class with Key and Value the types of keys and values respectively.
#include "dictcpp.hpp"
#include "tests/catch.hpp"
auto make_dictionary() {
auto dict = Dict<char, int>();
dict['a'] = 20;
dict['b'] = 100;
dict['c'] = 500;
return dict;
}
Dictionaries can also be initialised using initialiser list:
auto initialise() {
const auto dict = Dict<char, int>{
{'a', 1},
{'c', 2},
{'e', 3}
};
return dict;
}
Elements can be accessed using:
void each_item() {
const auto dict = Dict<char, int>{
{'a', 1},
{'c', 2},
{'e', 3},
{'a', 9}
};
for (const &[key, value]: dict.items()) {
assert(dict.at(key) == value);
}
}
std::vector<char> keys() {
const auto dict = Dict<char, int>{
{'a', 1},
{'c', 2},
{'e', 3},
{'a', 9}
};
return dict.keys();
}
std::vector<char> values() {
const auto dict = Dict<char, int>{
{'a', 1},
{'c', 2},
{'e', 3},
{'a', 9}
};
return dict.values();
}
For further information see the library documentation.