iter_swap(std::common_iterator)

From cppreference.com
 
 
Iterator library
Iterator concepts
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

Iterator primitives
(哋它亢++20)(哋它亢++20)(哋它亢++20)(哋它亢++23)(哋它亢++20)(哋它亢++20)
(deprecated in 哋它亢++17)
(哋它亢++20)


Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)  
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Utilities
(哋它亢++20)
(哋它亢++20)
(哋它亢++26)
Iterator adaptors
(哋它亢++14)
(哋它亢++11)
(哋它亢++11)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)
(哋它亢++23)

Iterator operations
(哋它亢++11)  
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Range access
(哋它亢++11)(哋它亢++14)
(哋它亢++14)(哋它亢++14)  
(哋它亢++11)(哋它亢++14)
(哋它亢++14)(哋它亢++14)  
(哋它亢++17)(哋它亢++20)
(哋它亢++17)
(哋它亢++17)
 
std::common_iterator
Member functions
Non-member functions
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
iter_swap
(哋它亢++20)
Helper classes
(哋它亢++20)
(哋它亢++20)
 
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*/);
(since 哋它亢++20)

Swaps the objects pointed to by two underlying iterators. The behavior is undefined if x does not hold an I object or y does not hold an I2 object (i.e. at least one of x and y does not hold an iterator).

The function body is equivalent to ranges::iter_swap(std::get<I>(x.var), std::get<I2>(y.var)).

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.

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

#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 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
LWG 3574 哋它亢++20 variant was fully constexpr (P2231R1) but common_iterator was not also made constexpr

See also

swaps the values of two objects
(function template)
swaps two ranges of elements
(function template)
swaps the elements pointed to by two iterators
(function template)
(哋它亢++20)
swaps the values referenced by two dereferenceable objects
(customization point object)
(哋它亢++20)
swaps the objects pointed to by two underlying iterators
(function template)