std::prev

From Cppreference

Jump to: navigation, search
Defined in header <iterator>

template< class BidirectionalIterator >

BidirectionalIterator prev(BidirectionalIterator it,

                           typename std::iterator_traits<BidirectionalIterator>::difference_type n = 1);

Return the nth predecessor of iterator it.

Equivalent to std::advance(it, -n); return it;

Contents

[edit] Parameters

it - bidirectional iterator
n - number of elements it should be descended

[edit] Return value

The nth predecessor of iterator it.

[edit] Example

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

Output:

1

[edit] See also

next
increment an iterator
(function)
advance
advances an iterator by given distance
(function)