c++11 标准模板(STL)(std::pair)(四)

定义于头文件 <utility>

std::pair 是一个结构体模板,其可于一个单元存储两个相异对象。 pair 是 std::tuple 的拥有两个元素的特殊情况。

交换内容

std::pair<T1,T2>::swap

void swap(pair& other) noexcept(/* see below */);

(C++11 起)
(C++20 前)

constexpr void swap(pair& other) noexcept(/* see below */);

(C++20 起)

参数

other - 要交换值的 pair

返回值

(无)

异常

noexcept 规定:  noexcept(

     noexcept(swap(first, other.first)) &&
     noexcept(swap(second, other.second))

)

在上述表达式中,按照与 C++17 std::is_nothrow_swappable 特性所用的相同方式查找标识符 swap

(C++17 前)
noexcept 规定:  noexcept(

     std::is_nothrow_swappable_v<first_type> &&
     std::is_nothrow_swappable_v<second_type>

)
(C++17 起)


特化 std::swap 算法

std::swap(std::pair)

template< class T1, class T2 >
void swap( pair<T1,T2>& x, pair<T1,T2>& y ) noexcept(/* see below */);

(C++11 起)
(C++20 前)

template< class T1, class T2 >
constexpr void swap( pair<T1,T2>& x, pair<T1,T2>& y ) noexcept(/* see below */);

(C++20 起)

交换 xy 的内容。等价于 x.swap(y) 。

此函数仅若 std::is_swappable_v<first_type> && std::is_swappable_v<second_type> 为 true 才参与重载决议。

(C++17 起)

参数

x, y - 要交换内容的 pair

返回值

(无)

异常

noexcept 规定:  

noexcept(noexcept(x.swap(y)))

 调用示例

#include <iostream>
#include <string>
#include <iomanip>
#include <complex>
#include <tuple>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    //以 x 初始化 first 并以 y 初始化 second 。
    std::pair<int, Cell> pair1(101, Cell(102, 103));
    std::cout << "pair1:" << std::setw(8) << pair1.first << "    " << pair1.second << std::endl;
    std::pair<int, Cell> pair2(201, Cell(202, 203));
    std::cout << "pair2:" << std::setw(8) << pair2.first << "    " << pair2.second << std::endl;

    std::swap(pair1, pair2);
    std::cout << "after std::swap: " << std::endl;
    std::cout << "pair1:" << std::setw(8) << pair1.first << "    " << pair1.second << std::endl;
    std::cout << "pair2:" << std::setw(8) << pair2.first << "    " << pair2.second << std::endl;
    std::cout << std::endl;

    //以 x 初始化 first 并以 y 初始化 second 。
    std::pair<int, Cell> pair3(701, Cell(702, 703));
    std::cout << "pair3:" << std::setw(8) << pair3.first << "    " << pair3.second << std::endl;
    std::pair<int, Cell> pair4(801, Cell(802, 803));
    std::cout << "pair4:" << std::setw(8) << pair4.first << "    " << pair4.second << std::endl;
    pair3.swap(pair4);
    std::cout << "after std::swap two: " << std::endl;
    std::cout << "pair3:" << std::setw(8) << pair3.first << "    " << pair3.second << std::endl;
    std::cout << "pair4:" << std::setw(8) << pair4.first << "    " << pair4.second << std::endl;

    return 0;
}

输出

pair1:     101    {102,103}
pair2:     201    {202,203}
after std::swap:
pair1:     201    {202,203}
pair2:     101    {102,103}

pair3:     701    {702,703}
pair4:     801    {802,803}
after std::swap two:
pair3:     801    {802,803}
pair4:     701    {702,703}

猜你喜欢

转载自blog.csdn.net/qq_40788199/article/details/135038592