定義于頭文件 <tuple>
template< class... Types > | (C++11 起) |
類模板 std::tuple
是固定大小的異類值匯集。它是 std::pair 的推廣。
若 (std::is_trivially_destructible_v<Types> && ...) 為 true ,則 | (C++17 起) |
模板形參
Types... | - | tuple 所存儲的元素的類型。支持空列表。 |
賦值一個 tuple 的內容給另一個
std::tuple<Types...>::operator=
tuple& operator=( const tuple& other ); | (1) | (C++11 起) (C++20 前) |
constexpr tuple& operator=( const tuple& other ); | (C++20 起) | |
tuple& operator=( tuple&& other ) noexcept(/* see below */); | (2) | (C++11 起) (C++20 前) |
constexpr tuple& operator=( tuple&& other ) noexcept(/* see below */); | (C++20 起) | |
template< class... UTypes > | (3) | (C++11 起) (C++20 前) |
template< class... UTypes > | (C++20 起) | |
template< class... UTypes > | (4) | (C++11 起) (C++20 前) |
template< class... UTypes > | (C++20 起) | |
template< class U1, class U2 > | (5) | (C++11 起) (C++20 前) |
template< class U1, class U2 > | (C++20 起) | |
template< class U1, class U2 > | (6) | (C++11 起) (C++20 前) |
template< class U1, class U2 > | (C++20 起) |
以另一 tuple
或 pair
的內容替換 tuple
的內容。
1) 復制賦值運算符。復制賦值 other
的每個元素給 *this 的對應元素。
2) 移動賦值運算符。對所有 i
,賦值 std::forward<Ti>(get<i>(other)) 給 get<i>(*this) 。
3) 對所有 i
,賦 std::get<i>(other) 給 std::get<i>(*this) 。
4) 對所有 i
,賦 std::forward<Ui>(std::get<i>(other)) 給 std::get<i>(*this) 。
5) 賦 p.first 給 *this 的首元素并賦 p.second 給 *this 的第二元素。
6) 賦 std::forward<U1>(p.first) 給 *this 的首元素并賦 std::forward<U2>(p.second) 給 *this 的第二元素。
?
這些函數的行為未定義,除非:
| (C++17 前) |
這些函數不參與重載決議(或對于復制賦值運算符,為定義為被刪除),若任何要求的賦值運算非法或若存在大小不匹配。特別是:
| (C++17 起) |
?
參數
other | - | 要替換此 tuple 內容的 tuple |
p | - | 要替換此 2-tuple 內容的 pair |
返回值
*this
異常
1) (無)
2)noexcept 規定:??
noexcept(is_nothrow_move_assignable<T0>::value &&is_nothrow_move_assignable<T1>::value &&is_nothrow_move_assignable<T2>::value &&...
)
3-6) (無)
調用示例
#include <tuple>
#include <iostream>
#include <string>
#include <typeinfo>
#include <stdexcept>
#include <iostream>
#include <vector>
#include <memory>struct Cell
{int x;int y;Cell() = default;Cell(int a, int b): x(a), y(b) {}Cell(const Cell &cell){x = cell.x;y = cell.y;}Cell &operator+(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator+=(const Cell &cell){x += cell.x;y += cell.y;return *this;}Cell &operator*=(int n){x *= n;y *= n;return *this;}Cell &operator++(){x += 1;y += 1;return *this;}friend Cell operator +(const Cell &cell1, const Cell &cell2){Cell cell = cell1;cell += cell2;return cell;}friend Cell operator *(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};return cell;}friend Cell operator /(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x / cell2.x, cell1.y / cell2.y};return cell;}friend Cell operator %(const Cell &cell1, const Cell &cell2){Cell cell = {cell1.x % cell2.x, cell1.y % cell2.y};return cell;}friend bool operator ==(const Cell &cell1, const Cell &cell2){return cell1.x == cell2.x && cell1.y == cell2.y;}friend bool operator !=(const Cell &cell1, const Cell &cell2){return cell1.x != cell2.x && cell1.y != cell2.y;}friend bool operator <(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y < cell2.y;}else{return cell1.x < cell2.x;}}friend bool operator >(const Cell &cell1, const Cell &cell2){if (cell1.x == cell2.x){return cell1.y > cell2.y;}else{return cell1.x > cell2.x;}}
};std::ostream &operator<<(std::ostream &os, const Cell &cell)
{os << "{" << cell.x << "," << cell.y << "}";return os;
}// 打印任何大小 tuple 的輔助函數
template<class Tuple, std::size_t N>
struct TuplePrinter
{static void print(const Tuple& t){TuplePrinter < Tuple, N - 1 >::print(t);std::cout << ", " << std::get < N - 1 > (t);}
};template<class Tuple>
struct TuplePrinter<Tuple, 1>
{static void print(const Tuple& t){std::cout << std::get<0>(t);}
};template<class... Args>
void print(const std::tuple<Args...>& t)
{std::cout << "(";TuplePrinter<decltype(t), sizeof...(Args)>::print(t);std::cout << ")" << std::endl;
}
// 輔助函數結束int main()
{std::tuple<Cell, std::string, double> tuple1(Cell{101, 101}, "GGX", 3.14);std::cout << "Initialized with values: ";print(tuple1);//以另一 tuple 或 pair 的內容替換 tuple 的內容。//1) 復制賦值運算符。復制賦值 other 的每個元素給 *this 的對應元素。std::tuple<Cell, std::string, double> tuple2 = tuple1;std::cout << "Assignment Operators: ";print(tuple2);//2) 移動賦值運算符。對所有 i ,賦值 std::forward<Ti>(get<i>(other)) 給 get<i>(*this) 。std::tuple<Cell, std::string, double> tuple3 = std::move(tuple1);std::cout << "Mobile assignment operator: ";print(tuple3);//3) 對所有 i ,賦 std::get<i>(other) 給 std::get<i>(*this) 。std::tuple<Cell, std::string, double> tuple4 =std::tuple<Cell, std::string, double>(Cell{101, 101}, "GGX", 3.14);std::cout << "Mobile assignment operator: ";print(tuple4);//4) 對所有 i ,賦 std::forward<Ui>(std::get<i>(other)) 給 std::get<i>(*this) 。std::tuple<Cell, std::string, double> tuple5 =std::move(std::tuple<Cell, std::string, double>(Cell{101, 101}, "GGX", 3.14));std::cout << "Mobile assignment operator: ";print(tuple5);//5) 賦 p.first 給 *this 的首元素并賦 p.second 給 *this 的第二元素。std::tuple<Cell, std::string> tuple6 = std::make_pair(Cell{1023, 1024}, "GGX");std::cout << "std::make_pair(): ";print(tuple6);//6) 賦 std::forward<U1>(p.first) 給 *this 的首元素并賦//std::forward<U2>(p.second) 給 *this 的第二元素。std::tuple<Cell, std::string> tuple7 = std::make_pair(Cell{1023, 1024}, "GGX");std::cout << "std::move(std::make_pair()): ";print(tuple7);return 0;
}
輸出
Initialized with values: ({101,101}, GGX, 3.14)
Assignment Operators: ({101,101}, GGX, 3.14)
Mobile assignment operator: ({101,101}, GGX, 3.14)
Mobile assignment operator: ({101,101}, GGX, 3.14)
Mobile assignment operator: ({101,101}, GGX, 3.14)
std::make_pair(): ({1023,1024}, GGX)
std::move(std::make_pair()): ({1023,1024}, GGX)