From 1107f8cd8235378aa32014d5789a8b36ff58cd06 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Fri, 21 Dec 2018 22:51:12 +0100 Subject: [PATCH] :memo: updated documentation for items() function --- include/nlohmann/json.hpp | 17 ++++++++++++++--- single_include/nlohmann/json.hpp | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index b75f4fef..a4cb15e3 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -4340,9 +4340,20 @@ class basic_json Range-based for loop with `items()` function: @code{cpp} - for (auto it : j_object.items()) + for (auto& el : j_object.items()) { - std::cout << "key: " << it.key() << ", value:" << it.value() << '\n'; + std::cout << "key: " << el.key() << ", value:" << el.value() << '\n'; + } + @endcode + + The `items()` function also allows to use + [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding) + (C++17): + + @code{cpp} + for (auto& [key, val] : j_object.items()) + { + std::cout << "key: " << key << ", value:" << val << '\n'; } @endcode @@ -4360,7 +4371,7 @@ class basic_json @complexity Constant. - @since version 3.1.0. + @since version 3.1.0, structured bindings support since 3.5.0. */ iteration_proxy items() noexcept { diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 5341affd..5eed0474 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -16720,9 +16720,20 @@ class basic_json Range-based for loop with `items()` function: @code{cpp} - for (auto it : j_object.items()) + for (auto& el : j_object.items()) { - std::cout << "key: " << it.key() << ", value:" << it.value() << '\n'; + std::cout << "key: " << el.key() << ", value:" << el.value() << '\n'; + } + @endcode + + The `items()` function also allows to use + [structured bindings](https://en.cppreference.com/w/cpp/language/structured_binding) + (C++17): + + @code{cpp} + for (auto& [key, val] : j_object.items()) + { + std::cout << "key: " << key << ", value:" << val << '\n'; } @endcode @@ -16740,7 +16751,7 @@ class basic_json @complexity Constant. - @since version 3.1.0. + @since version 3.1.0, structured bindings support since 3.5.0. */ iteration_proxy items() noexcept {