std::iter_swap(3)
π
std::iter_swap(3) π
okay bestie, welcome to the std::iter_swap(3) page from the C++ Standard Libary manual. this one is short, sweet, and swaps stuff. β¨
π NAME π
std::iter_swap - std::iter_swap. yes literally, the name is the name. iconic. π
π¦ Synopsis π¦
Defined in header <algorithm>
template< class ForwardIt1, class ForwardIt2 > (constexpr since C++20)
void iter_swap( ForwardIt1 a, ForwardIt2 b );
Swaps the values of the elements the given iterators are pointing to. π«Ά
If any of the following conditions is satisfied, the behavior is undefined β not cute, not safe, do not pass go:
aorbis not dereferenceable.*ais notSwappablewith*b.
π Parameters π
a,bβ iterators to the elements to swap.
πΈ Type requirements πΈ
ForwardIt1,ForwardIt2must meet the requirements ofLegacyForwardIterator.
β¨ Return value β¨
(none) β she returns nothing. no notes. π
π₯ Complexity π₯
Constant. literally constant, no dramatic growth arc.
π 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.
π§ Possible implementation π§
template<class ForwardIt1, class ForwardIt2>
constexpr //< since C++20
void iter_swap(ForwardIt1 a, ForwardIt2 b)
{
using std::swap;
swap(*a, *b);
}
π Example π
The following is an implementation of selection sort in C++. Thereβs a // Run this code link in the original, and hereβs the whole thing:
#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
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR:LWG 187; Applied to:C++98; Behavior as published: it was unspecified whetherswapis used; Correct behavior: the effect is equivalent toswap(*a, *b).
π¦ See also π¦
swapβ swaps the values of two objects (function template).swap_rangesβ swaps two ranges of elements (function template).iter_swapβ swaps the objects pointed to by two adjusted underlying iterators (C++20) (function template).iter_swapβ swaps the objects pointed to by two underlying iterators (C++20) (function template).iter_swapβ swaps the values referenced by two dereferenceable objects (C++20) (customization point object).
source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::iter_swap(3) β bye bestie, go swap responsibly! π
π¦ 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