std::next(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::next(3) β€” C++ Standard Libary, bestie πŸ’…

okay so welcome to std::next, the iterator-advancing icon of the C++ Standard Library. This is the std::next(3) manual page, and we're gonna get you comfy with it. no cap. ✨


🌸 NAME 🌸

std::next - std::next


✨ Synopsis ✨

Defined in header <iterator>.

The page lays out the synopsis like this, version labels and all β€” I'm keeping it exactly as it appears:

Defined in header <iterator>
template< class InputIt >
(since C++11)
InputIt next( InputIt it, typename
(until C++17)
std::iterator_traits<InputIt>::difference_type n = 1 );
template< class InputIt >

constexpr
(since C++17)

InputIt next( InputIt it, typename
std::iterator_traits<InputIt>::difference_type n = 1 );

Return the n^th successor (or -n^th predecessor if n is negative) of iterator it.


πŸ“ Parameters πŸ“

  • it β€” an iterator
  • n β€” number of elements to advance

πŸŽ€ Type requirements πŸŽ€

  • InputIt must meet the requirements of LegacyInputIterator.

πŸ¦‹ Return value πŸ¦‹

An iterator of type InputIt that holds the n^th successor (or -n^th predecessor if n is negative) of iterator it.


⏱️ Complexity ⏱️

Linear.

However, if InputIt additionally meets the requirements of LegacyRandomAccessIterator, complexity is constant.


πŸ› οΈ Possible implementation πŸ› οΈ

template<class InputIt>
constexpr // since C++17
InputIt next(InputIt it, typename
std::iterator_traits<InputIt>::difference_type n = 1)
{
    std::advance(it, n);
    return it;
}

πŸ“ Notes πŸ“

Although the expression ++c.begin() often compiles, it is not guaranteed to do so: c.begin() is an rvalue expression, and there is no LegacyInputIterator requirement that specifies that increment of an rvalue is guaranteed to work. In particular, when iterators are implemented as pointers or its operator++ is lvalue-ref-qualified, ++c.begin() does not compile, while std::next(c.begin()) does.


🌈 Example 🌈

The page says // Run this code right before the example:

#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::vector<int> v{4, 5, 6};

    auto it = v.begin();
    auto nx = std::next(it, 2);
    std::cout << *it << ' ' << *nx << '\n';

    it = v.end();
    nx = std::next(it, -2);
    std::cout << ' ' << *nx << '\n';
}

πŸ–₯️ Output:

4 6
 5

😭 Defect reports 😭

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • DR LWG 2353 β€” applied to C++11 β€” behavior as published: next required LegacyForwardIterator; correct behavior: LegacyInputIterator allowed.

πŸ‘€ See also πŸ‘€

  • prev (C++11) (function template) β€” decrement an iterator
  • advance (function template) β€” advances an iterator by given distance
  • distance (function template) β€” returns the distance between two iterators
  • ranges::next (C++20) (niebloid) β€” increment an iterator by a given distance or to a bound

okay that's the whole page, bestie. this one came from http://cppreference.com, dated 2024.06.10, and it's the std::next(3) page. go advance some iterators responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from