std::bidire...nal_iterator(3)
π std::bidirectional_iterator β C++ Standard Libary manual π
okay so bestie, welcome to the std::bidirectional_iterator page from the C++ Standard Libary manual. this one is literally about an iterator that can move backward, and we're gonna get through it together π«Άβ¨
β¨ NAME β¨
std::bidirectional_iterator β std::bidirectional_iterator
(yes, the name is the name, iconic behavior π)
πΈ Synopsis πΈ
here's the literal synopsis, header line and all, no cap:
Defined in header <iterator>
template< class I >
concept bidirectional_iterator =
std::forward_iterator<I> &&
std::derived_from</*ITER_CONCEPT*/<I>,
std::bidirectional_iterator_tag> &&
(since C++20)
requires(I i) {
{ --i } -> std::same_as<I&>;
{ i-- } -> std::same_as<I>;
};
The concept bidirectional_iterator refines forward_iterator by adding the ability to move an iterator backward. that's the whole glow-up: forward iterator core, plus backward movement π
π¦ Notes π¦
Unlike the LegacyBidirectionalIterator requirements, the bidirectional_iterator concept does not require dereference to return an lvalue. read that again bestie: not required to return an lvalue ππ
π Example π
A minimum bidirectional iterator. here's the full code, exact and all:
#include <cstddef>
#include <iterator>
struct SimpleBidiIterator
{
using difference_type = std::ptrdiff_t;
using value_type = int;
int operator*() const;
SimpleBidiIterator& operator++();
SimpleBidiIterator operator++(int)
{
auto tmp = *this;
++*this;
return tmp;
}
SimpleBidiIterator& operator--();
SimpleBidiIterator operator--(int)
{
auto tmp = *this;
--*this;
return tmp;
}
bool operator==(const SimpleBidiIterator&) const;
};
static_assert(std::bidirectional_iterator<SimpleBidiIterator>);
and that's the whole minimum bidirectional iterator flex β¨π¦
π See also π
forward_iterator(C++20) β specifies that aninput_iteratoris a forward iterator, supporting equality comparison and multi-pass (concept).random_access_iterator(C++20) β specifies that abidirectional_iteratoris a random-access iterator, supporting advancement in constant time and subscripting (concept).
source:http://cppreference.comΒ· version/date:2024.06.10Β· page:std::bidire...nal_iterator(3)β that's the cute little sign-off, mwah π
π¦ 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