Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/reverse iterator/iter swap"

From cppreference.com
m (fmt, .)
({{source}} -> {{c multi}}.)
 
Line 1: Line 1:
{{cpp/title|iter_swap<small>(std::reverse_iterator)</small>}}
+
{{cpp/title|iter_swap(std::reverse_iterator)}}
 
{{cpp/iterator/reverse_iterator/navbar}}
 
{{cpp/iterator/reverse_iterator/navbar}}
{{dcl begin}}
+
{{|since=c++20|
{{dcl|since=c++20|
+
 
template< std::indirectly_swappable<Iter> Iter2 >
 
template< std::indirectly_swappable<Iter> Iter2 >
    friend constexpr void
+
friend constexpr void iter_swap( const reverse_iterator& x,
        iter_swap( const reverse_iterator& x,
+
const std::reverse_iterator<Iter2>& y )
                  const std::reverse_iterator<Iter2>& y ) noexcept(/*see below*/);
+
noexcept(/* see below */);
 
}}
 
}}
{{dcl end}}
 
  
Swaps the objects pointed to by two adjusted underlying iterators. The function body is equivalent to:
+
Swaps the objects pointed to by two adjusted underlying iterators.
  
{{source|1=
+
{{|auto tmp_x =x.base();auto tmp_y =y.base();ranges::iter_swap(--tmp_x, --tmp_y);}}
auto tmp_x = x.base();
+
auto tmp_y = y.base();
+
ranges::iter_swap(--tmp_x, --tmp_y);
+
}}
+
  
{{cpp/hidden friend|std::reverse_iterator<Iter>|tmpl=yes}}
+
{{cpp/hidden friend|std::reverse_iterator<Iter>|tmpl=yes}}
  
 
===Parameters===
 
===Parameters===
Line 24: Line 18:
 
{{par|x, y|reverse iterators to the elements to swap}}
 
{{par|x, y|reverse iterators to the elements to swap}}
 
{{par end}}
 
{{par end}}
 
===Return value===
 
(none)
 
  
 
===Complexity===
 
===Complexity===
Line 50: Line 41:
 
     std::vector v{1, 2, 3};
 
     std::vector v{1, 2, 3};
 
     std::list l{4, 5, 6};
 
     std::list l{4, 5, 6};
 
+
 
     std::reverse_iterator<std::vector<int>::iterator> r1{v.rbegin()};
 
     std::reverse_iterator<std::vector<int>::iterator> r1{v.rbegin()};
 
     std::reverse_iterator<std::list<int>::iterator> r2{l.rbegin()};
 
     std::reverse_iterator<std::list<int>::iterator> r2{l.rbegin()};
 
+
 
     std::cout << *r1 << ' ' << *r2 << '\n';
 
     std::cout << *r1 << ' ' << *r2 << '\n';
 
+
 
     iter_swap(r1, r2); // ADL
 
     iter_swap(r1, r2); // ADL
 
+
 
     std::cout << *r1 << ' ' << *r2 << '\n';
 
     std::cout << *r1 << ' ' << *r2 << '\n';
 
}
 
}

Latest revision as of 23:29, 4 November 2024

 
 
Iterator library
Iterator concepts
Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
 
template< std::indirectly_swappable<Iter> Iter2 >

friend constexpr void iter_swap( const reverse_iterator& x,
                                 const std::reverse_iterator<Iter2>& y )

    noexcept(/* see below */);
(since C++20)

Swaps the objects pointed to by two adjusted underlying iterators.

Equivalent to auto tmp_x = x.base();
auto tmp_y = y.base();
ranges::iter_swap(--tmp_x, --tmp_y);
.

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.

Contents

[edit] Parameters

x, y - reverse iterators to the elements to swap

[edit] Complexity

Constant.

[edit] Exceptions

noexcept specification:  
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&>()))

)

[edit] Example

#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

[edit] See also

swaps the values of two objects
(function template) [edit]
swaps two ranges of elements
(function template) [edit]
swaps the elements pointed to by two iterators
(function template) [edit]
(C++20)
swaps the values referenced by two dereferenceable objects
(customization point object)[edit]
(C++20)
swaps the objects pointed to by two underlying iterators
(function template) [edit]