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

std::pair::swap

void swap(pair& other);

(since C++11)

掉期first带着other.firstsecond带着other.second...

This function does not participate in overload resolution unless std::is_swappable_v<first_type> && std::is_swappable_v<second_type>

(since C++17)

参数

other

-

pair of values to swap

返回值

%280%29

例外

二次

代码语言:javascript
复制
#include <iostream>
#include <utility>
#include <string>
int main()
{
    std::pair<int, std::string> p1, p2;
    p1 = std::make_pair(10, "test");
    p2.swap(p1);
    std::cout << "(" << p2.first << ", " << p2.second << ")\n";
}

二次

产出:

二次

代码语言:javascript
复制
(10, test)

二次

另见

noexcept specification: noexcept( noexcept(std::swap(first, other.first)) && noexcept(std::swap(second, other.second)). )

(until C++17)

noexcept specification: noexcept( std::is_nothrow_swappable<first_type>::value) && std::is_nothrow_swappable<second_type>::value). )

(since C++17)

swap

swaps the values of two objects (function template)

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

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

扫码关注腾讯云开发者

领取腾讯云代金券