Skip to content

Commit 47202c8

Browse files
authored
Add missing overload to ordered_map::find (#5171)
* 🐛 add missing overload Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🎨 fix amalgamation Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🐛 fix typo Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 🚨 fix warning Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
1 parent 4e5fa3b commit 47202c8

3 files changed

Lines changed: 33 additions & 0 deletions

File tree

include/nlohmann/ordered_map.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,20 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
340340
return Container::end();
341341
}
342342

343+
template<class KeyType, detail::enable_if_t<
344+
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
345+
const_iterator find(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
346+
{
347+
for (auto it = this->begin(); it != this->end(); ++it)
348+
{
349+
if (m_compare(it->first, key))
350+
{
351+
return it;
352+
}
353+
}
354+
return Container::end();
355+
}
356+
343357
std::pair<iterator, bool> insert( value_type&& value )
344358
{
345359
return emplace(value.first, std::move(value.second));

single_include/nlohmann/json.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20468,6 +20468,20 @@ template <class Key, class T, class IgnoredLess = std::less<Key>,
2046820468
return Container::end();
2046920469
}
2047020470

20471+
template<class KeyType, detail::enable_if_t<
20472+
detail::is_usable_as_key_type<key_compare, key_type, KeyType>::value, int> = 0>
20473+
const_iterator find(KeyType && key) const // NOLINT(cppcoreguidelines-missing-std-forward)
20474+
{
20475+
for (auto it = this->begin(); it != this->end(); ++it)
20476+
{
20477+
if (m_compare(it->first, key))
20478+
{
20479+
return it;
20480+
}
20481+
}
20482+
return Container::end();
20483+
}
20484+
2047120485
std::pair<iterator, bool> insert( value_type&& value )
2047220486
{
2047320487
return emplace(value.first, std::move(value.second));

tests/src/unit-ordered_map.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ TEST_CASE("ordered_map")
269269
CHECK(com.find("vier") == com.end());
270270
CHECK(com.find(std::string("vier")) == com.end());
271271
CHECK(com.find(vier) == com.end());
272+
273+
#ifdef JSON_HAS_CPP_17
274+
CHECK(om.find(std::string_view("eins")) == om.begin());
275+
CHECK(com.find(std::string_view("eins")) == com.begin());
276+
#endif
272277
}
273278

274279
SECTION("insert")

0 commit comments

Comments
 (0)