C++ Logo


Advanced search

Subject: [std-proposals] Finding the index of an element (std::find_first_index)
From: Kosher Yosher (goekhan.oezdogan_at_[hidden])
Date: 2020-10-01 10:42:53


Quick thoughts on adding a new function to <algorithm> which returns the
index of an element, matched by a predicate, in a container? The goal is to
stop writing FindX scanning functions and make it as concise as possible to
do this common operation.


auto i = std::find_first_index(v.begin(), v.end(), [](const auto& e){
return e.someMember == 1337; });
if (i != std::invalid_index)
// Do something with i.


constexpr std::size_t invalid_index = std::numeric_limits<std::size_t>::max();

 template<class InputIterator, class UnaryPredicate>
constexpr std::size_t find_first_index(InputIterator first,
InputIterator last, UnaryPredicate pred)
    std::size_t i = 0;
    while (first != last)
        if (pred(*first))
            return i;
    } return std::invalid_index;

STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups