Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/iter swap"

From cppreference.com
< cpp‎ | algorithm
m (Capitalized 1st letter, ., +const&)
(Wording update.)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{cpp/title|iter_swap}}
 
{{cpp/title|iter_swap}}
 
{{cpp/algorithm/navbar}}
 
{{cpp/algorithm/navbar}}
{{dcl begin}}
+
{{ddcl|header=algorithm|notes={{mark constexpr since c++20}}|
{{dcl header|algorithm}}
+
{{dcl rev multi|until1=c++20|dcl1=
+
 
template< class ForwardIt1, class ForwardIt2 >
 
template< class ForwardIt1, class ForwardIt2 >
 
void iter_swap( ForwardIt1 a, ForwardIt2 b );
 
void iter_swap( ForwardIt1 a, ForwardIt2 b );
|dcl2=
 
template< class ForwardIt1, class ForwardIt2 >
 
constexpr void iter_swap( ForwardIt1 a, ForwardIt2 b );
 
 
}}
 
}}
{{dcl end}}
 
  
 
Swaps the values of the elements the given iterators are pointing to.
 
Swaps the values of the elements the given iterators are pointing to.
 +
 +
If any of the following conditions is satisfied, the behavior is undefined:
 +
* {{c|a}} or {{c|b}} is not [[cpp/iterator#Dereferenceability and validity|dereferenceable]].
 +
* {{c|*a}} is not {{named req|Swappable}} with {{c|*b}}.
  
 
===Parameters===
 
===Parameters===
Line 19: Line 17:
 
{{par hreq}}
 
{{par hreq}}
 
{{par req named|ForwardIt1, ForwardIt2|ForwardIterator}}
 
{{par req named|ForwardIt1, ForwardIt2|ForwardIterator}}
{{par req named|{{c|*a}}, {{c|*b}}|Swappable}}
 
 
{{par end}}
 
{{par end}}
  
Line 34: Line 31:
 
{{eq fun|1=
 
{{eq fun|1=
 
template<class ForwardIt1, class ForwardIt2>
 
template<class ForwardIt1, class ForwardIt2>
constexpr void iter_swap(ForwardIt1 a, ForwardIt2 b) // constexpr since C++20
+
constexpr //< since C++20
 +
void iter_swap(ForwardIt1 a, ForwardIt2 b)
 
{
 
{
 
     using std::swap;
 
     using std::swap;
Line 46: Line 44:
 
|code=
 
|code=
 
#include <algorithm>
 
#include <algorithm>
#include <functional>
 
 
#include <iostream>
 
#include <iostream>
 
#include <random>
 
#include <random>
 +
#include <string_view>
 
#include <vector>
 
#include <vector>
  
Line 54: Line 52:
 
void selection_sort(ForwardIt begin, ForwardIt end)
 
void selection_sort(ForwardIt begin, ForwardIt end)
 
{
 
{
     for (ForwardIt i = begin; i != end; ++i)
+
     for (ForwardIt it = begin; it != end; ++it)
         std::iter_swap(i, std::min_element(i, end));
+
         std::iter_swap(it, std::min_element(it, end));
 +
}
 +
 
 +
void println(std::string_view rem, std::vector<int> const& v)
 +
{
 +
    std::cout << rem;
 +
    for (int e : v)
 +
        std::cout << e << ' ';
 +
    std::cout << '\n';
 +
}
 +
 
 +
template<int min, int max>
 +
int rand_int()
 +
{
 +
    static std::uniform_int_distribution dist(min, max);
 +
    static std::mt19937 gen(std::random_device{}());
 +
    return dist(gen);
 
}
 
}
  
 
int main()
 
int main()
 
{
 
{
    std::random_device rd;
 
    std::mt19937 gen(rd());
 
    std::uniform_int_distribution<> dist(-9, +9);
 
 
     std::vector<int> v;
 
     std::vector<int> v;
     std::generate_n(back_inserter(v), 20, bind(dist, gen));
+
     std::generate_n(std::back_inserter(v), 20, rand_int<-9, +9>);
   
+
    std::cout << "Before sort: " << std::showpos;
+
    for (const auto& e : v)
+
        std::cout << e << ' ';
+
 
      
 
      
 +
    std::cout << std::showpos;
 +
    println("Before sort: ", v);
 
     selection_sort(v.begin(), v.end());
 
     selection_sort(v.begin(), v.end());
      
+
     println("After sort: ", v);
    std::cout << "\nAfter sort : ";
+
    for (const auto& e : v)
+
        std::cout << e << ' ';
+
    std::cout << '\n';
+
 
}
 
}
 
|p=true
 
|p=true
 
|output=
 
|output=
 
Before sort: -9 -3 +2 -8 +0 -1 +8 -4 -5 +1 -4 -5 +4 -9 -8 -6 -6 +8 -4 -6  
 
Before sort: -9 -3 +2 -8 +0 -1 +8 -4 -5 +1 -4 -5 +4 -9 -8 -6 -6 +8 -4 -6  
After sort : -9 -9 -8 -8 -6 -6 -6 -5 -5 -4 -4 -4 -3 -1 +0 +1 +2 +4 +8 +8
+
After sort: -9 -9 -8 -8 -6 -6 -6 -5 -5 -4 -4 -4 -3 -1 +0 +1 +2 +4 +8 +8
 
}}
 
}}
  

Latest revision as of 23:49, 25 March 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
iter_swap
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
 
Defined in header <algorithm>
template< class ForwardIt1, class ForwardIt2 >
void iter_swap( ForwardIt1 a, ForwardIt2 b );
(constexpr since C++20)

Swaps the values of the elements the given iterators are pointing to.

If any of the following conditions is satisfied, the behavior is undefined:

Contents

[edit] Parameters

a, b - iterators to the elements to swap
Type requirements
-
ForwardIt1, ForwardIt2 must meet the requirements of LegacyForwardIterator.

[edit] Return value

(none)

[edit] Complexity

Constant.

[edit] Notes

This function template models the semantics of the swap operation given by Swappable. That is, overloads of swap found by ADL and the fall back of std::swap are considered.

[edit] Possible implementation

template<class ForwardIt1, class ForwardIt2>
constexpr //< since C++20
void iter_swap(ForwardIt1 a, ForwardIt2 b)
{
    using std::swap;
    swap(*a, *b);
}

[edit] Example

The following is an implementation of selection sort in C++.

#include <algorithm>
#include <iostream>
#include <random>
#include <string_view>
#include <vector>
 
template<class ForwardIt>
void selection_sort(ForwardIt begin, ForwardIt end)
{
    for (ForwardIt it = begin; it != end; ++it)
        std::iter_swap(it, std::min_element(it, end));
}
 
void println(std::string_view rem, std::vector<int> const& v)
{
    std::cout << rem;
    for (int e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
 
template<int min, int max>
int rand_int()
{
    static std::uniform_int_distribution dist(min, max);
    static std::mt19937 gen(std::random_device{}());
    return dist(gen);
}
 
int main()
{
    std::vector<int> v;
    std::generate_n(std::back_inserter(v), 20, rand_int<-9, +9>);
 
    std::cout << std::showpos;
    println("Before sort: ", v);
    selection_sort(v.begin(), v.end());
    println("After sort:  ", v);
}

Possible output:

Before sort: -9 -3 +2 -8 +0 -1 +8 -4 -5 +1 -4 -5 +4 -9 -8 -6 -6 +8 -4 -6 
After sort:  -9 -9 -8 -8 -6 -6 -6 -5 -5 -4 -4 -4 -3 -1 +0 +1 +2 +4 +8 +8

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 187 C++98 it was unspecified whether swap is used the effect is equivalent to swap(*a, *b)

[edit] See also

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