std::pair<T1,T2>::operator=

来自cppreference.com
< cpp‎ | utility‎ | pair
 
 
工具库
通用工具
日期和时间
函数对象
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
初等字符串转换
(C++17)
(C++17)
 
std::pair
成员函数
pair::operator=
(C++11)
非成员函数
(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20 前)(C++20)
(C++11)
(C++11)
推导指引(C++17)
辅助类
(C++11)
 
(1)
pair& operator=( const pair& other );
(C++20 前)
constexpr pair& operator=( const pair& other );
(C++20 起)
constexpr const pair& operator=( const pair& other ) const;
(2) (C++23 起)
(3)
template< class U1, class U2 >
pair& operator=( const pair<U1, U2>& other );
(C++11 起)
(C++20 前)
template< class U1, class U2 >
constexpr pair& operator=( const pair<U1, U2>& other );
(C++20 起)
template< class U1, class U2 >
constexpr const pair& operator=( const pair<U1, U2>& other ) const;
(4) (C++23 起)
(5)
pair& operator=( pair&& other ) noexcept(/* see below */);
(C++11 起)
(C++20 前)
constexpr pair& operator=( pair&& other ) noexcept(/* see below */);
(C++20 起)
constexpr const pair& operator=( pair&& other ) const;
(6) (C++23 起)
(7)
template< class U1, class U2 >
pair& operator=( pair<U1, U2>&& other );
(C++11 起)
(C++20 前)
template< class U1, class U2 >
constexpr pair& operator=( pair<U1, U2>&& other );
(C++20 起)
template< class U1, class U2 >
constexpr const pair& operator=( pair<U1, U2>&& other ) const;
(8) (C++23 起)

替换 pair 的内容。

1) 复制赋值运算符。以 other 内容的副本替换内容。
  • 此赋值运算符被隐式声明。若 T1T2 之一为 const 限定类型,或引用类型,或拥有不可访问的复制赋值运算符的类类型,或这种类的数组类型,则使用此赋值运算符令程序为谬构。
(C++11 前)
(C++11 起)
2) const 限定操作数的复制赋值运算符。
3) 赋值 other.firstfirstother.secondsecond
4) 赋值 other.firstfirstother.secondsecond
5) 移动赋值运算符。用移动语义以 other 的内容替换内容。
6) const 限定操作数的移动赋值运算符。
7) 赋值 std::forward<U1>(p.first)firststd::forward<U2>(p.second)second
8) 赋值 std::forward<U1>(p.first)firststd::forward<U2>(p.second)second

参数

other - 替换此 pair 的值的 pair

返回值

*this

异常

1-4) 可能抛出实现定义的异常。
5)
noexcept 说明:  
noexcept(

    std::is_nothrow_move_assignable<T1>::value &&
    std::is_nothrow_move_assignable<T2>::value

)
6-8) 可能抛出实现定义的异常。

示例

#include <iomanip>
#include <iostream>
#include <utility>
#include <vector>
 
template <class Os, class T>
Os& operator<<(Os& os, const std::vector<T>& v) {
    os << "{";
    for (std::size_t t = 0; t != v.size(); ++t)
        os << v[t] << (t+1 < v.size() ? "," : "");
    return os << "}";
}
 
template <class Os, class U1, class U2>
Os& operator<<(Os& os, const std::pair<U1, U2>& pair) {
    return os << ":{ " << pair.first << ", " << pair.second << " } ";
}
 
int main()
{
    std::pair<int, std::vector<int>> p{ 1, {2} }, q{ 2, {5,6} };
 
    p = q; // (1) operator=( const pair& other );
    std::cout << std::setw(23) << std::left
              << "(1) p = q;" << "p" << p << "   q" << q << '\n';
 
    std::pair<short, std::vector<int>> r{ 4, {7,8,9} };
    p = r; // (3) operator=( const pair<U1,U2>& other );
    std::cout << std::setw(23)
              << "(3) p = r;" << "p" << p << " r" << r << '\n';
 
    p = std::pair<int, std::vector<int>>{ 3, {4} };
    p = std::move(q); // (5) operator=( pair&& other );
    std::cout << std::setw(23)
              << "(5) p = std::move(q);" << "p" << p << "   q" << q << '\n';
 
    p = std::pair<int, std::vector<int>>{ 5, {6} };
    p = std::move(r); // (7) operator=( pair<U1,U2>&& other );
    std::cout << std::setw(23)
              << "(7) p = std::move(r);" << "p" << p << " r" << r << '\n';
}

输出:

(1) p = q;             p:{ 2, {5,6} }    q:{ 2, {5,6} } 
(3) p = r;             p:{ 4, {7,8,9} }  r:{ 4, {7,8,9} } 
(5) p = std::move(q);  p:{ 2, {5,6} }    q:{ 2, {} } 
(7) p = std::move(r);  p:{ 4, {7,8,9} }  r:{ 4, {} }

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 2729 C++11 pair::operator= 未被约束并可能导致不必要的未定义行为 已约束

参阅

(C++11)
赋值一个 tuple 的内容给另一个
(std::tuple<Types...> 的公开成员函数)