Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/next"

From cppreference.com
< cpp‎ | iterator
m (Shorten template names. Use {{lc}} where appropriate.)
m (Update links.)
Line 56: Line 56:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/iterator/dcl list prev}}
+
{{dsc inc | cpp/iterator/dsc prev}}
{{dsc inc | cpp/iterator/dcl list advance}}
+
{{dsc inc | cpp/iterator/dsc advance}}
 
{{dsc end}}
 
{{dsc end}}
  

Revision as of 22:05, 31 May 2013

 
 
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)
 
Defined in header <iterator>
template< class ForwardIt >

ForwardIt next( ForwardIt it,

                typename std::iterator_traits<ForwardIt>::difference_type n = 1 );
(since C++11)

Return the nth successor of iterator it.

Contents

Parameters

it - an iterator
n - number of elements to advance
Type requirements

Template:par req concept

Return value

The nth successor of iterator it.

Possible implementation

template<class ForwardIt>
ForwardIt next(ForwardIt it, typename std::iterator_traits<ForwardIt>::difference_type n = 1)
{
    std::advance(it, n);
    return it;
}

Example

#include <iostream>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{ 3, 1, 4 };
 
    auto it = v.begin();
 
    auto nx = std::next(it, 2);
 
    std::cout << *it << ' ' << *nx << '\n';
}

Output:

3 4

See also

(C++11)
decrement an iterator
(function template) [edit]
advances an iterator by given distance
(function template) [edit]