Namespaces
Variants

std::ranges:: next

From cppreference.net
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11) (C++14)
(C++14) (C++14)
(C++11) (C++14)
(C++14) (C++14)
(C++17) (C++20)
(C++17)
(C++17)
Défini dans l'en-tête <iterator>
Signature d'appel
template < std:: input_or_output_iterator I >
constexpr I next ( I i ) ;
(1) (depuis C++20)
template < std:: input_or_output_iterator I >
constexpr I next ( I i, std:: iter_difference_t < I > n ) ;
(2) (depuis C++20)
template < std:: input_or_output_iterator I, std:: sentinel_for < I > S >
constexpr I next ( I i, S bound ) ;
(3) (depuis C++20)
template < std:: input_or_output_iterator I, std:: sentinel_for < I > S >
constexpr I next ( I i, std:: iter_difference_t < I > n, S bound ) ;
(4) (depuis C++20)

Retourne le n ème successeur de l'itérateur i .

Les entités de type fonction décrites sur cette page sont des objets fonction d'algorithmes (informellement appelés niebloids ), c'est-à-dire :

Table des matières

Paramètres

i - un itérateur
n - nombre d'éléments à avancer
bound - sentinelle désignant la fin de la plage i pointe vers

Valeur de retour

1) Le successeur de l'itérateur i .
2) Le n ème successeur de l'itérateur i .
3) Le premier itérateur équivalent à bound .
4) Le n ème successeur de l'itérateur i , ou le premier itérateur équivalent à bound , selon celui qui arrive en premier.

Complexité

1) Constante.
2) Constant si I modélise std::random_access_iterator ; sinon linéaire.
3) Constant si I et S modélisent à la fois std:: random_access_iterator < I > et std:: sized_sentinel_for < S, I > , ou si I et S modélisent std:: assignable_from < I & , S > ; sinon linéaire.
4) Constant si I et S modélisent tous deux std:: random_access_iterator < I > et std:: sized_sentinel_for < S, I > ; sinon linéaire.

Implémentation possible

struct next_fn
{
    template<std::input_or_output_iterator I>
    constexpr I operator()(I i) const
    {
        ++i;
        return i;
    }
    template<std::input_or_output_iterator I>
    constexpr I operator()(I i, std::iter_difference_t<I> n) const
    {
        ranges::advance(i, n);
        return i;
    }
    template<std::input_or_output_iterator I, std::sentinel_for<I> S>
    constexpr I operator()(I i, S bound) const
    {
        ranges::advance(i, bound);
        return i;
    }
    template<std::input_or_output_iterator I, std::sentinel_for<I> S>
    constexpr I operator()(I i, std::iter_difference_t<I> n, S bound) const
    {
        ranges::advance(i, n, bound);
        return i;
    }
};
inline constexpr auto next = next_fn();

Notes

Bien que l'expression ++ x. begin ( ) compile souvent, rien ne garantit qu'elle le fasse : x. begin ( ) est une expression rvalue, et aucune exigence ne spécifie que l'incrément d'une rvalue est garanti de fonctionner. En particulier, lorsque les itérateurs sont implémentés comme des pointeurs ou que leur operator++ est qualifié de référence lvalue, ++ x. begin ( ) ne compile pas, tandis que ranges :: next ( x. begin ( ) ) le fait.

Exemple

#include <cassert>
#include <iterator>
int main() 
{
    auto v = {3, 1, 4};
    {
        auto n = std::ranges::next(v.begin());
        assert(*n == 1);
    }
    {
        auto n = std::ranges::next(v.begin(), 2);
        assert(*n == 4);
    }
    {
        auto n = std::ranges::next(v.begin(), v.end());
        assert(n == v.end());
    }
    {
        auto n = std::ranges::next(v.begin(), 42, v.end());
        assert(n == v.end());
    }
}

Voir aussi

décrémente un itérateur d'une distance donnée ou jusqu'à une limite
(objet fonction algorithme)
avance un itérateur d'une distance donnée ou jusqu'à une limite donnée
(objet fonction algorithme)
(C++11)
incrémente un itérateur
(modèle de fonction)