From 47202c804ad9be5dbb97f9f0d24242ff570ba67d Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 18 May 2026 20:33:47 +0200 Subject: [PATCH] Add missing overload to ordered_map::find (#5171) * :bug: add missing overload Signed-off-by: Niels Lohmann * :art: fix amalgamation Signed-off-by: Niels Lohmann * :bug: fix typo Signed-off-by: Niels Lohmann * :rotating_light: fix warning Signed-off-by: Niels Lohmann --------- Signed-off-by: Niels Lohmann --- include/nlohmann/ordered_map.hpp | 14 ++++++++++++++ single_include/nlohmann/json.hpp | 14 ++++++++++++++ tests/src/unit-ordered_map.cpp | 5 +++++ 3 files changed, 33 insertions(+) diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index 5ba0da18f..7b8cf70f4 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -340,6 +340,20 @@ template , return Container::end(); } + template::value, int> = 0> + const_iterator find(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward) + { + for (auto it = this->begin(); it != this->end(); ++it) + { + if (m_compare(it->first, key)) + { + return it; + } + } + return Container::end(); + } + std::pair insert( value_type&& value ) { return emplace(value.first, std::move(value.second)); diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7fb20c6c9..795d60cfe 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20468,6 +20468,20 @@ template , return Container::end(); } + template::value, int> = 0> + const_iterator find(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward) + { + for (auto it = this->begin(); it != this->end(); ++it) + { + if (m_compare(it->first, key)) + { + return it; + } + } + return Container::end(); + } + std::pair insert( value_type&& value ) { return emplace(value.first, std::move(value.second)); diff --git a/tests/src/unit-ordered_map.cpp b/tests/src/unit-ordered_map.cpp index 76ed4f34f..f380a9869 100644 --- a/tests/src/unit-ordered_map.cpp +++ b/tests/src/unit-ordered_map.cpp @@ -269,6 +269,11 @@ TEST_CASE("ordered_map") CHECK(com.find("vier") == com.end()); CHECK(com.find(std::string("vier")) == com.end()); CHECK(com.find(vier) == com.end()); + +#ifdef JSON_HAS_CPP_17 + CHECK(om.find(std::string_view("eins")) == om.begin()); + CHECK(com.find(std::string_view("eins")) == com.begin()); +#endif } SECTION("insert")