std::iter_s...se_iterator)(3)
π NAME π
Okay so welcome to the C++ Standard Libary page for std::iter_swap(std::reverse_iterator) β manual name is spelled C++ Standard Libary exactly like that, and the page label/header thingy is std::iter_s...se_iterator)(3). Weβre not fixing the typo, weβre honoring it. π
std::iter_swap(std::reverse_iterator) β thatβs the whole name. Periodt. π«Ά
β¨ Synopsis β¨
This overload is here since C++20, and the synopsis is literally this:
template< std::indirectly_swappable<Iter> Iter2 >
friend constexpr void
iter_swap( const reverse_iterator& x,
(since C++20)
const std::reverse_iterator<Iter2>& y )
noexcept(/*see below*/);
What it does? Swaps the objects pointed to by two adjusted underlying iterators. The function body is equivalent to:
auto tmp_x = x.base();
auto tmp_y = y.base();
ranges::iter_swap(--tmp_x, --tmp_y);
π¨ Real talk, bestie: this function template is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::reverse_iterator<Iter> is an associated class of the arguments. Thatβs the whole rule, no cap.
πΈ Parameters πΈ
x,yβ reverse iterators to the elements to swap β¨
π Return value π
(none) β nothing comes back, it just does the swap. Iconic behavior. π«
π§ Complexity π§
Constant. Thatβs it. No secret extra work, fr fr.
π Exceptions π
The noexcept specification is exactly this:
noexcept(
std::is_nothrow_copy_constructible_v<Iter> &&
std::is_nothrow_copy_constructible_v<Iter2> &&
noexcept(ranges::iter_swap(--std::declval<Iter&>(),
--std::declval<Iter2&>()))
)
π Example π
// Run this code
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
int main()
{
std::vector v{1, 2, 3};
std::list l{4, 5, 6};
std::reverse_iterator<std::vector<int>::iterator> r1{v.rbegin()};
std::reverse_iterator<std::list<int>::iterator> r2{l.rbegin()};
std::cout << *r1 << ' ' << *r2 << '\n';
iter_swap(r1, r2); // ADL
std::cout << *r1 << ' ' << *r2 << '\n';
}
π₯οΈ Output π₯οΈ
3 6
6 3
π See also π
swapβ swaps the values of two objects (function template)swap_rangesβ swaps two ranges of elements (function template)iter_swapβ swaps the elements pointed to by two iterators (function template)iter_swapβ swaps the values referenced by two dereferenceable objects (C++20) (customization point object)iter_swapβ swaps the objects pointed to by two underlying iterators (C++20) (function template)
π Category π
Category: * conditionally noexcept π
Thatβs the whole page, bestie. Source/version: http://cppreference.com β’ date: 2024.06.10 β’ page label: std::iter_s...se_iterator)(3). 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