std::swap

From Cppreference

Jump to: navigation, search
Defined in header <algorithm>
Defined in header <utility>
(pre-C++11 version)
(C++11 version)

template< class T >
void swap( T& a, T& b );
(1)
template< class T, size_t N >
void swap( T (&a)[N], T (&b)[N]);
(2) (C++11 feature)

Exchanges the given values.

1) Swaps the values a and b.

2) Swaps the arrays a and b. In effect calls std::swap_ranges(a, a+N, b).

Contents

[edit] Parameters

a, b - the values to be swapped

[edit] Return value

(none)

[edit] Exceptions

1) noexcept specification:  (C++11 feature)

noexcept(noexcept(

    std::is_nothrow_move_constructible<T>::value &&
    std::is_nothrow_move_assignable<T>::value

))

2) noexcept specification:  (C++11 feature)

noexcept(noexcept(swap(*a, *b)))

[edit] Complexity

Constant

[edit] Specializations

Custom specializations of the std::swap algorithm are allowed. The following specializations are already provided by the standard library:

std::swap(std::pair) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::tuple) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::shared_ptr) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::weak_ptr) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::unique_ptr) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::function) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_string)
specializes the std::swap() algorithm
(function template)
std::swap(std::array)
specializes the std::swap() algorithm
(function template)
std::swap(std::deque)
specializes the std::swap() algorithm
(function template)
std::swap(std::forward_list)
specializes the std::swap() algorithm
(function template)
std::swap(std::list)
specializes the std::swap() algorithm
(function template)
std::swap(std::vector)
specializes the std::swap() algorithm
(function template)
std::swap(std::map)
specializes the std::swap() algorithm
(function template)
std::swap(std::multimap)
specializes the std::swap() algorithm
(function template)
std::swap(std::set)
specializes the std::swap() algorithm
(function template)
std::swap(std::multiset)
specializes the std::swap() algorithm
(function template)
std::swap(std::unordered_map)
specializes the std::swap() algorithm
(function template)
std::swap(std::unordered_multimap)
specializes the std::swap() algorithm
(function template)
std::swap(std::unordered_set)
specializes the std::swap() algorithm
(function template)
std::swap(std::unordered_multiset)
specializes the std::swap() algorithm
(function template)
std::swap(std::queue)
specializes the std::swap() algorithm
(function template)
std::swap(std::priority_queue)
specializes the std::swap() algorithm
(function template)
std::swap(std::stack)
specializes the std::swap() algorithm
(function template)
std::swap(std::valarray) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_stringbuf) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_istringstream) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_ostringstream) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_stringstream) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_filebuf) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_ifstream) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_ofstream) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_fstream) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::basic_regex) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::match_results) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::thread) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::unique_lock) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::promise) (C++11)
specializes the std::swap() algorithm
(function template)
std::swap(std::packaged_task) (C++11)
specializes the std::swap() algorithm
(function template)

[edit] Example

[edit] See also

iter_swap
swaps the elements pointed to by two iterators
(function template)
swap_ranges
swaps two ranges of elements
(function template)