Namespaces
Variants
Views
Actions

std::prev

From cppreference.com
Defined in header <iterator>
template< class BidirectionalIterator >

BidirectionalIterator prev(BidirectionalIterator it,

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

Return the nth predecessor of iterator it.

Equivalent to std::advance(it, -n); return it;, that is, advances a copy of the iterator it in the reverse direction.

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

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