std::bidire...nal_iterator(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 an input_iterator is a forward iterator, supporting equality comparison and multi-pass (concept).
  • random_access_iterator (C++20) β€” specifies that a bidirectional_iterator is 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