Boost logo

Boost :

From: Vinnie Falco (vinnie.falco_at_[hidden])
Date: 2024-12-17 16:06:44


On Tue, Dec 17, 2024 at 8:00 AM Neil Groves <neilgroves_at_[hidden]> wrote:
> It doesn't even need to be limited to containers. I think it generalizes to ranges. Isn't the free function along the lines of auto find_or(Rng&& rng, Key&& key, Default&& default) ? This works for all containers that allow the default to be something other than nullopt, which is very useful in many contexts. Where there are associative containers it can delegate to the optimal container implementation, but it can fall back to the general logic of std::ranges::find. A production version would, I assume, have the projection and other usual range idioms.

Working with ranges is an interesting idea, and comparisons to
std::ranges::find might encounter obstacles. I presume that the OP was
talking about returning an optional value given the key. This presumes
an associative container. I am not too familiar with ranges, is there
an "associative range" concept? I do like the default value idea
though, thanks for that :)

    template< typename AssociativeContainer, typename Key, typename
DefaultValue = std::nullopt >
    auto try_find( AssociativeContainer&& c, Key&& k, DefaultValue&&
alternative = {} ) ->
        std::optional< typename AssociativeContainer::mapped_type >;

Thanks


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