首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::swap

Defined in header <algorithm> Defined in header <utility>

?

(until C++11)(since C++11)

template< class T > void swap( T& a, T& b );

(1)

?

template< class T2, std::size_t N > void swap( T2 (&a)N, T2 (&b)N);

(2)

(since C++11)

交换给定的值。

1%29交换值ab此重载不参与过载解决,除非std::is_move_constructible_v<T>&&std::is_move_assignable_v<T>true.%28自C++17%29

2%29交换数组ab.实际上是打电话std::swap_ranges(a, a+N, b)此重载不参与过载解决,除非std::is_swappable_v<T2>true.%28自C++17%29

参数

a, b

-

the values to be swapped

类型要求

-T必须符合可转让和可移动的要求。

-T2必须符合可互换的要求。

返回值

%280%29

例外

1%29

(none)

(until C++11)

noexcept specification: noexcept( std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value. )

(since C++11)

2%29

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

(until C++17)

noexcept specification: noexcept(std::is_nothrow_swappable_v<T2>)

(since C++17)

复杂性

1%29常数

2%29线性在N中

专门性

std::swap可能是专攻命名空间STD对于用户定义的类型,但是这些专门化并不是由ADL%28命名空间std不是用户定义类型%29的关联命名空间。使用户定义的类型可交换的预期方法是在与类型相同的命名空间中提供非成员函数交换:Swappable关于细节。

标准库已经提供了以下重载:

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) (C++11)

specializes the std::swap algorithm (function template)

std::swap(std::deque)

specializes the std::swap algorithm (function template)

std::swap(std::forward_list) (C++11)

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) (C++11)

specializes the std::swap algorithm (function template)

std::swap(std::unordered_multimap) (C++11)

specializes the std::swap algorithm (function template)

std::swap(std::unordered_set) (C++11)

specializes the std::swap algorithm (function template)

std::swap(std::unordered_multiset) (C++11)

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)

specialization of std::swap for unique_lock (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)

std::swap(std::optional) (C++17)

specializes the std::swap algorithm (function)

std::swap(std::any) (C++17)

specializes the std::swap algorithm (function)

std::swap(std::variant) (C++17)

specializes the std::swap algorithm (function)

swap(std::filesystem::path)

swaps two paths (function)

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
 
int main()
{
   int a = 5, b = 3;
 
   // before
   std::cout << a << ' ' << b << '\n';
 
   std::swap(a,b);
 
   // after
   std::cout << a << ' ' << b << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
5 3
3 5

二次

另见

iter_swap

swaps the elements pointed to by two iterators (function template)

swap_ranges

swaps two ranges of elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com