From 9633e5fe5a7a7aa64a542debe02b1a37496850bc Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 21 Jun 2020 14:11:26 +0900 Subject: [PATCH] doc: add iteration examples into as_xxx section related to #120 --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 2c7f1a4..9629ebd 100644 --- a/README.md +++ b/README.md @@ -465,6 +465,24 @@ if(answer.is_integer() && answer.as_integer(std::nothrow) == 42) If `std::nothrow` is passed, the functions are marked as noexcept. +By casting a `toml::value` into an array or a table, you can iterate over the +elements. + +```cpp +const auto data = toml::parse("example.toml"); +std::cout << "keys in the top-level table are the following: \n"; +for(const auto& [k, v] : data.as_table()) +{ + std::cout << k << '\n'; +} + +const auto& fruits = toml::find(data, "fruits"); +for(const auto& v : fruits.as_array()) +{ + std::cout << toml::find(v, "name") << '\n'; +} +``` + The full list of the functions is below. ```cpp