iter_swap(s...d_iterator)(3)
β¨ NAME β¨
okay so bestie, welcome to iter_swap(s...d_iterator)(3), straight out of the C++ Standard Libary manual (yes, βLibaryβ is literally how itβs spelled on the page, and honestly? iconic). This is the std::counted_iterator flavor of iter_swap, and it swaps the objects pointed to by two underlying iterators. π
πΈ Synopsis πΈ
The page lays the synopsis out all wrapped and chaotic, so here is the exact block, bestie:
template< std::indirectly_swappable<I> I2 >
friend constexpr void
iter_swap( const counted_iterator& x, const
(since C++20)
std::counted_iterator<I2>& y )
noexcept(noexcept(ranges::iter_swap(x.base(),
y.base())));
That (since C++20) marker means this overload is since C++20, and the signature is a friend constexpr void iter_swap( const counted_iterator& x, const std::counted_iterator<I2>& y ) with noexcept(noexcept(ranges::iter_swap(x.base(), y.base()))). The template constraint is template< std::indirectly_swappable<I> I2 >. π«Ά
It swaps the objects pointed to by two underlying iterators. π¨ Undefined behavior alert: the behavior is undefined if either x.count() or y.count() is equal to 0 β like, do not do that, bestie. π
The function body is equivalent to: ranges::iter_swap(x.base(), y.base());.
This function template is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::counted_iterator<I> is an associated class of the arguments. iykyk π
π Parameters π
x,yβ iterator adaptors to the elements to swap
π Return value π
(none)
π¦ Complexity π¦
Constant. no cap, itβs giving efficiency.
π Example π
The page has the classic // Run this code line, so hereβs the code, exactly:
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
int main()
{
std::vector p{1, 2, 3, 4},
q{5, 6, 7, 8};
std::counted_iterator<std::vector<int>::iterator> ip{p.begin(), 2};
std::counted_iterator<std::vector<int>::iterator> iq{q.begin(), 3};
std::cout << *ip << ' ' << *iq << '\n';
iter_swap(ip, iq); // ADL
std::cout << *ip << ' ' << *iq << '\n';
std::list x{0, 1, 3};
std::counted_iterator<std::list<int>::iterator> ix{x.begin(), 2};
// iter_swap(ip, ix); // error: not indirectly swappable
}
β Output β
1 5
5 1
π₯ 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(C++20) β swaps the values referenced by two dereferenceable objects (customization point object)iter_move(C++20) β casts the result of dereferencing the underlying iterator to its associated rvalue reference type (function)
kisses, your fave C++ bestie π signing off with source/version http://cppreference.com and date 2024.06.10 π«Άβ¨
π¦ 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