std::make_reverse_iterator(3)

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

✨ std::make_reverse_iterator ✨

okay so bestie, welcome to the C++ Standard Library manual page for std::make_reverse_iterator β€” yes, that's the whole NAME section, it's literally just std::make_reverse_iterator twice, iconic minimalism honestly πŸ’…


πŸŽ€ Synopsis πŸŽ€

Defined in header <iterator>
template< class Iter >                                    (since C++14)
std::reverse_iterator<Iter> make_reverse_iterator( Iter i );
                                              (constexpr since C++17)

So like. make_reverse_iterator is a convenience function template that constructs a std::reverse_iterator for the iterator i you hand it β€” and i must be a LegacyBidirectionalIterator, no exceptions, that's the vibe check it has to pass β€” with the type deduced from the type of the argument. Deduction queen behavior fr fr 🫢


🌸 Parameters 🌸

  • i β€” the iterator being converted into a reverse iterator. that's it. that's the whole parameter list. she's low maintenance πŸ’–

πŸ¦‹ Return value πŸ¦‹

A std::reverse_iterator constructed from i. slay, that's the whole deal ✨


⭐ Notes ⭐

there's a feature-test macro you can check, and here's the tea:

  • Feature-test macro: __cpp_lib_make_reverse_iterator
  • Value: 201402L (C++14)
  • Feature: std::make_reverse_iterator

ngl knowing this macro exists is such main character energy πŸ‘‘


πŸ’œ Possible implementation πŸ’œ

like, this is what it could look like under the hood, and it's giving "so simple it's almost rude":

template<class Iter>
constexpr std::reverse_iterator<Iter> make_reverse_iterator(Iter i)
{
    return std::reverse_iterator<Iter>(i);
}

one-line body. one singular return. ate and left no crumbs πŸ”₯


🌈 Example 🌈

// Run this code β€” go on, run it, I'll wait 🧚

#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::vector<int> v{1, 3, 10, 8, 22};

    std::sort(v.begin(), v.end());
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, ", "));
    std::cout << '\n';

    std::copy(std::make_reverse_iterator(v.end()),
              std::make_reverse_iterator(v.begin()),
              std::ostream_iterator<int>(std::cout, ", "));
    std::cout << '\n';
}

πŸ“ Output πŸ“

1, 3, 8, 10, 22,
22, 10, 8, 3, 1,

notice how we're passing v.end() first and v.begin() second? that's the reverse-iterator moment bestie, that's the whole personality πŸ˜­πŸ’€


πŸ‘‘ See also πŸ‘‘

  • reverse_iterator β€” iterator adaptor for reverse-order traversal (class template)
  • rbegin β€” returns a reverse iterator to the beginning of a container or array (function template) (C++14)
  • crbegin β€” (function template) (C++14)
  • rend β€” returns a reverse end iterator for a container or array (function template) (C++14)
  • crend β€” (function template) (C++14)
heads up bestie: the c-prefixed ones (crbegin, crend) show up as plain (function template) (C++14) in the original page, so that's exactly how I'm giving them to you β€” no invented descriptions, no cap πŸ™ˆ

and that's the page!! brought to you by http://cppreference.com, version 2024.06.10 β€” yell at them, not me, if anything's weird. mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from