std::ranges::adjacent_find
Depuis fr.cppreference.net
| Défini dans l'en-tête <algorithm>
|
||
| Signature d'appel |
||
template< std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<I, Proj>,
std::projected<I, Proj>> Pred = ranges::equal_to >
constexpr I
adjacent_find( I first, S last, Pred pred = {}, Proj proj = {} );
|
(1) | (depuis C++20) |
template< ranges::forward_range R, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<ranges::iterator_t<R>, Proj>,
std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to >
constexpr ranges::borrowed_iterator_t<R>
adjacent_find( R&& r, Pred pred = {}, Proj proj = {} );
|
(2) | (depuis C++20) |
Recherche dans la plage [first, last) les deux premiers éléments consécutifs égaux.
1) Les éléments sont comparés en utilisant
pred (après projection avec la projection proj).2) Identique à (1), mais utilise
r comme plage source, comme si ranges::begin(r) était utilisé comme first et ranges::end(r) comme last.Les entités de type fonction décrites sur cette page sont des objets fonctions d'algorithme (informellement connus sous le nom de niebloids), c'est-à-dire :
- Les listes d'arguments de modèle explicites ne peuvent pas être spécifiées lors de l'appel de l'une d'entre elles.
- Aucune d'entre elles n'est visible par la recherche dépendante des arguments.
- Lorsque l'une d'entre elles est trouvée par recherche non qualifiée normale comme nom à gauche de l'opérateur d'appel de fonction, la recherche dépendante des arguments est inhibée.
Paramètres
| first, last | - | la paire itérateur-sentinelle définissant la plage d'éléments à examiner |
| r | - | la plage des éléments à examiner |
| pred | - | prédicat à appliquer aux éléments projetés |
| proj | - | projection à appliquer aux éléments |
Valeur de retour
Un itérateur vers le premier de la première paire d'éléments identiques, c'est-à-dire le premier itérateur it tel que bool(std::invoke(pred, std::invoke(proj1, *it), std::invoke(proj, *(it + 1)))) est true.
Si aucun élément de ce type n'est trouvé, un itérateur égal à last est renvoyé.
Complexité
Exactement min((result - first) + 1, (last - first) - 1) applications du prédicat et de la projection où result est la valeur de retour.
Implémentation possible
struct adjacent_find_fn
{
template<std::forward_iterator I, std::sentinel_for<I> S, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<I, Proj>,
std::projected<I, Proj>> Pred = ranges::equal_to>
constexpr I operator()(I first, S last, Pred pred = {}, Proj proj = {}) const
{
if (first == last)
return first;
auto next = ranges::next(first);
for (; next != last; ++next, ++first)
if (std::invoke(pred, std::invoke(proj, *first), std::invoke(proj, *next)))
return first;
return next;
}
template<ranges::forward_range R, class Proj = std::identity,
std::indirect_binary_predicate<
std::projected<ranges::iterator_t<R>, Proj>,
std::projected<ranges::iterator_t<R>, Proj>> Pred = ranges::equal_to>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r, Pred pred = {}, Proj proj = {}) const
{
return (*this)(ranges::begin(r), ranges::end(r), std::ref(pred), std::ref(proj));
}
};
inline constexpr adjacent_find_fn adjacent_find;
|
Exemple
Exécuter ce code
#include <algorithm>
#include <functional>
#include <iostream>
#include <ranges>
constexpr bool some_of(auto&& r, auto&& pred) // some but not all
{
return std::ranges::cend(r) != std::ranges::adjacent_find(r,
[&pred](auto const& x, auto const& y)
{
return pred(x) != pred(y);
});
}
// test some_of
constexpr auto a = {0, 0, 0, 0}, b = {1, 1, 1, 0}, c = {1, 1, 1, 1};
auto is_one = [](auto x){ return x == 1; };
static_assert(!some_of(a, is_one) && some_of(b, is_one) && !some_of(c, is_one));
int main()
{
const auto v = {0, 1, 2, 3, 40, 40, 41, 41, 5}; /*
^^ ^^ */
namespace ranges = std::ranges;
if (auto it = ranges::adjacent_find(v.begin(), v.end()); it == v.end())
std::cout << "No matching adjacent elements\n";
else
std::cout << "The first adjacent pair of equal elements is at ["
<< ranges::distance(v.begin(), it) << "] == " << *it << '\n';
if (auto it = ranges::adjacent_find(v, ranges::greater()); it == v.end())
std::cout << "The entire vector is sorted in ascending order\n";
else
std::cout << "The last element in the non-decreasing subsequence is at ["
<< ranges::distance(v.begin(), it) << "] == " << *it << '\n';
}
Sortie :
The first adjacent pair of equal elements is at [4] == 40
The last element in the non-decreasing subsequence is at [7] == 41
Voir aussi
(C++20) |
supprime les éléments consécutifs en double dans une plage (objet fonction d'algorithme) |
| trouve les deux premiers éléments adjacents qui sont égaux (ou satisfont un prédicat donné) (modèle de fonction) |