Difference between revisions of "cpp/iterator/next"
From cppreference.com
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/ | + | {{dsc inc | cpp/iterator/dsc prev}} |
− | {{dsc inc | cpp/iterator/ | + | {{dsc inc | cpp/iterator/dsc advance}} |
{{dsc end}} | {{dsc end}} | ||
Revision as of 22:05, 31 May 2013
Defined in header <iterator>
|
||
template< class ForwardIt > ForwardIt next( ForwardIt it, |
(since C++11) | |
Return the nth successor of iterator it.
Contents |
Parameters
it | - | an iterator |
n | - | number of elements to advance |
Type requirements |
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
Run this code
#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) |
advances an iterator by given distance (function template) |