From 408c608fc7bf1557ee987dd7fbe662fabed21a53 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Mon, 15 Apr 2024 07:39:03 +0200 Subject: Adding upstream version 1.1.1. Signed-off-by: Daniel Baumann --- examples/value_modification.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/value_modification.cpp (limited to 'examples/value_modification.cpp') diff --git a/examples/value_modification.cpp b/examples/value_modification.cpp new file mode 100644 index 0000000..2584efd --- /dev/null +++ b/examples/value_modification.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +/// MAYBE_CONSTINIT expands to `constinit` if available. +#if __cpp_constinit +#define MAYBE_CONSTINIT constinit +#else +#define MAYBE_CONSTINIT +#endif + +// To make a frozen::unordered_map where you can modify the values, make a +// non-constexpr instance. If available, prefer to use constinit. It will +// initialize the map during compilation and detect any exceptions. +MAYBE_CONSTINIT static frozen::unordered_map fruits = { + {"n_apples", 0}, + {"n_pears", 0}, +}; + +int main() { + // Update the values using at() + fruits.at("n_apples") = 10; + fruits.at("n_pears") = fruits.at("n_apples") * 2; + std::cout << "n_apples: " << fruits.at("n_apples") << std::endl; + std::cout << "n_pears: " << fruits.at("n_pears") << std::endl; + + // You can also update values via the iterator returned by find() + auto found = fruits.find("n_apples"); + found->second = 0; + std::cout << "n_apples: " << fruits.at("n_apples") << std::endl; + + // Range also works + auto range = fruits.equal_range("n_apples"); + range.first->second = 1337; + std::cout << "n_apples: " << fruits.at("n_apples") << std::endl; +} \ No newline at end of file -- cgit v1.2.3