std::prev(3)
π std::prev β C++ Standard Libary (std::prev(3)) π
okay bestie, welcome to the std::prev(3) page β yes, the manual name says C++ Standard Libary, typo and all, and weβre keeping it π. This is the whole vibe: std::prev is that iconic little iterator-time-traveler, and sheβs here to help you go backward (or forward if n is negative, plot twist β¨).
πΈ NAME πΈ
std::prev β std::prev (yes bestie, thatβs the whole name, periodt π
).
β¨ Synopsis β¨
Defined in header <iterator>.
(since C++11) / (until C++17) β the first shape of this icon:
template< class BidirIt >
BidirIt prev( BidirIt it, typename
std::iterator_traits<BidirIt>::difference_type n = 1 );
(since C++17) β now with constexpr energy π:
template< class BidirIt >
constexpr
BidirIt prev( BidirIt it, typename
std::iterator_traits<BidirIt>::difference_type n = 1 );
Return the n^th predecessor (or -n^th successor if n is negative) of iterator it.
π«Ά Parameters π«Ά
itβ an iterator. literally the iterator weβre moving from π¦nβ number of elements it should be descended. lowkey the step count, bestie β¨
π Type requirements π
BidirItmust meet the requirements ofLegacyBidirectionalIterator. vibe check: itβs gotta be bidirectional, no cap π
π Return value π
An iterator of type BidirIt that holds the n^th predecessor (or -n^th successor if n is negative) of iterator it. itβs giving βsame iterator type, new positionβ π₯
β Complexity β
Linear. highkey expected for a nice little walk through the container πΆββοΈ
However, if BidirIt additionally meets the requirements of LegacyRandomAccessIterator, complexity is constant. so if itβs a random-access bestie, she teleports β‘
π οΈ Possible implementation π οΈ
okay this is the little implementation moment, and Iβm obsessed:
template<class BidirIt>
constexpr // since C++17
BidirIt prev(BidirIt it, typename
std::iterator_traits<BidirIt>::difference_type n = 1)
{
std::advance(it, -n);
return it;
}
std::advance(it, -n); and then return it; β simple, clean, iconic π
π Notes π
β οΈ bestie, listen: although the expression--c.end()often compiles, it is not guaranteed to do so.c.end()is an rvalue expression, and there is no iterator requirement that specifies that decrement of an rvalue is guaranteed to work. In particular, when iterators are implemented as pointers or itsoperator--is lvalue-ref-qualified,--c.end()does not compile, whilestd::prev(c.end())does. fr fr, this is the βusestd::prevand breatheβ moment ππ
π Example π
// Run this code
#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';
it = v.begin();
pv = std::prev(it, -2);
std::cout << *pv << '\n';
}
Output π
1
4
ate. no crumbs. π β¨
π See also π
nextβ increment an iterator (C++11) (function template) π¦advanceβ advances an iterator by given distance (function template) πΈdistanceβ returns the distance between two iterators (function template) πranges::prevβ decrement an iterator by a given distance or to a bound (C++20) (niebloid) π
mwah π β source: http://cppreference.com, version/date: 2024.06.10, page: std::prev(3). stay iterating, bestie π«Άπ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms