Boost logo

Boost :

Subject: [boost] Advanced find() for associative containers
From: Boris Rasin (boris_at_[hidden])
Date: 2017-03-16 17:18:22


I've written an improved version of find() for associative containers
(in the form of a free function), and I need your help answering these
two questions:

1. Will it be a useful addition to boost?
2. Where exactly should it fit in the boost?

Public interface consists of a single free function template:

template <typename C>
auto find(C& c, const typename C::key_type& key);

If element is found, return value evaluates to true and contains
iterator to found element. If element is not found, return value
evaluates to false and contains hint iterator for insert/emplace.

Allowing one to do this:

if (auto i = find(container, key))
{
     i->value_member();
     container.erase(i);
}

And this:

if (auto i = !find(container, key))
{
     container.insert(i, expensive_value_construction());
}

The code is here:
https://github.com/Boris-Rasin/find

I've tested it with the following containers:

std::map
std::multimap
std::unordered_map
std::set
std::multiset
std::unordered_set
boost::container::map
boost::container::set
boost::container::flat_map
boost::container::flat_set
boost::multi_index::ordered_unique
boost::multi_index::ordered_non_unique
boost::multi_index::hashed_unique
boost::multi_index::hashed_non_unique
boost::multi_index::ranked_unique
boost::multi_index::ranked_non_unique

Thanks for your comments and help.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk