iter_swap(s...on_iterator)(3)
π NAME π
Hey bestie! This is the iter_swap(std::common_iterator) page from the C++ Standard Libary manual, and the name is literally iter_swap(std::common_iterator) β iter_swap(std::common_iterator). Consistent queen behavior, honestly π
β¨
πΈ SYNOPSIS πΈ
template< std::indirectly_swappable<I> I2, class S2 >
friend constexpr void
iter_swap( const common_iterator& x,
const std::common_iterator<I2, S2>& y ) noexcept(/*see below*/);
This one is (since C++20) π
Swaps the objects pointed to by two underlying iterators. Swaps the objects pointed to by two underlying iterators. Yes, that's the whole vibe of the operation.
β οΈ The behavior is undefined ifxdoes not hold anIobject orydoes not hold anI2object (i.e. at least one ofxandydoes not hold an iterator). Like, please do not test fate with this one π
The function body is equivalent to ranges::iter_swap(std::get<I>(x.var), std::get<I2>(y.var)).
Important lookup tea: this function template is not visible to ordinary unqualified or qualified lookup, and can only be found by argument-dependent lookup when std::common_iterator<I,S> is an associated class of the arguments. iykyk π«Ά
π PARAMETERS π
x,yβ the iterators to the elements to swap
π« RETURN VALUE π«
(none)
β‘ COMPLEXITY β‘
Constant.
π‘οΈ EXCEPTIONS π‘οΈ
noexcept specification:
noexcept(noexcept(ranges::iter_swap(std::declval<const I&>(), std::declval<const I2&>())))
π§ EXAMPLE π§
// Run this code
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> v1{"1", "2", "3", "4", "5"},
v2{"Ξ±", "Ξ²", "Ξ³", "Ξ΄", "Ξ΅"};
using CI = std::common_iterator<
std::counted_iterator<std::vector<std::string>::iterator>,
std::default_sentinel_t
>;
CI first1{std::counted_iterator{v1.begin(), 3}};
CI first2{std::counted_iterator{v2.begin(), 4}};
CI last{std::default_sentinel};
auto print = [&](auto rem)
{
std::cout << rem << "v1 = ";
std::ranges::copy(v1,
std::ostream_iterator<std::string>{std::cout, " "});
std::cout << "\nv2 = ";
std::ranges::copy(v2,
std::ostream_iterator<std::string>{std::cout, " "});
std::cout << '\n';
};
print("Before iter_swap:\n");
for (; first1 != last && first2 != last; ++first1, ++first2)
iter_swap(first1, first2); // ADL
print("After iter_swap:\n");
}
Output
Before iter_swap:
v1 = 1 2 3 4 5
v2 = Ξ± Ξ² Ξ³ Ξ΄ Ξ΅
After iter_swap:
v1 = Ξ± Ξ² Ξ³ 4 5
v2 = 1 2 3 Ξ΄ Ξ΅
π©Ή DEFECT REPORTS π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 3574β applied to C++20. Behavior as published:variantwas fullyconstexpr(P2231R1) butcommon_iteratorwas not. Correct behavior: also madeconstexpr.
π 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 π·οΈ
conditionally noexcept
Source/version: http://cppreference.com Β· date: 2024.06.10 β go swap something cute and be the main character of your own iterator story, bestie ππ«Άβ¨
π¦ 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