summaryrefslogtreecommitdiffstats
path: root/examples/value_modification.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/value_modification.cpp')
-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