summaryrefslogtreecommitdiffstats
path: root/examples/value_modification.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 05:39:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-15 05:39:03 +0000
commit408c608fc7bf1557ee987dd7fbe662fabed21a53 (patch)
tree8b07135336de378134bfedc808d49747174810d3 /examples/value_modification.cpp
parentInitial commit. (diff)
downloadfrozen-408c608fc7bf1557ee987dd7fbe662fabed21a53.tar.xz
frozen-408c608fc7bf1557ee987dd7fbe662fabed21a53.zip
Adding upstream version 1.1.1.upstream/1.1.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--examples/value_modification.cpp37
1 files changed, 37 insertions, 0 deletions
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 <frozen/set.h>
+#include <frozen/string.h>
+#include <frozen/unordered_map.h>
+#include <iostream>
+
+/// 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<frozen::string, int, 2> 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