c++11 標準模板(STL)(std::tuple)(三)

定義于頭文件 <tuple>

template< class... Types >
class tuple;

(C++11 起)

類模板 std::tuple 是固定大小的異類值匯集。它是 std::pair 的推廣。

若 (std::is_trivially_destructible_v<Types> && ...) 為 true ,則 tuple 的析構函數是平凡的。

(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 >
tuple& operator=( const tuple<UTypes...>& other );

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

template< class... UTypes >
constexpr tuple& operator=( const tuple<UTypes...>& other );

(C++20 起)

template< class... UTypes >
tuple& operator=( tuple<UTypes...>&& other );

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

template< class... UTypes >
constexpr tuple& operator=( tuple<UTypes...>&& other );

(C++20 起)

template< class U1, class U2 >
tuple& operator=( const pair<U1,U2>& p );

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

template< class U1, class U2 >
constexpr tuple& operator=( const pair<U1,U2>& p );

(C++20 起)

template< class U1, class U2 >
tuple& operator=( pair<U1,U2>&& p );

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

template< class U1, class U2 >
constexpr tuple& operator=( pair<U1,U2>&& p );

(C++20 起)

以另一 tuplepair 的內容替換 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 的第二元素。

?

這些函數的行為未定義,除非:

  • 對于 (1) , std::is_copy_assignable<T_i>::value 對 Types 中所有 T_i 為 true 。
  • 對于 (2) , std::is_move_assignable<T_i>::value 對 Types 中所有 T_i 為 true 。
  • 對于 (3) , sizeof...(UTypes) == sizeof...(Types) 且 std::is_assignable<T_i&, const U_i&>::value 對所有 Types 中的 T_iUTypes 中的 U_i 的對應對為 true 。
  • 對于 (4) , sizeof...(UTypes) == sizeof...(Types) 且 std::is_assignable<T_i&, U_i&&>::value 對所有 Types 中的 T_iUTypes 中的 U_i 的對應對為 true 。
  • 對于 (5) , sizeof...(Types) == 2 且 std::is_assignable<T_0&, const U1&>::value 與 std::is_assignable<T_1&, const U2&>::value 都是 true ,其中 T_0T_1 是組成 Types 的二個類型。
  • 對于 (6) , sizeof...(Types) == 2 且 std::is_assignable<T_0&, U1&&>::value 與 std::is_assignable<T_1&, U2&&>::value 都是 true ,其中 T_0T_1 是組成 Types 的二個類型。
(C++17 前)

這些函數不參與重載決議(或對于復制賦值運算符,為定義為被刪除),若任何要求的賦值運算非法或若存在大小不匹配。特別是:

  • (1) 定義為被刪除,除非 std::is_copy_assignable<T_i>::value 對 Types 中所有 T_i 為 true 。
  • (2) 不參與重載決議,除非 std::is_move_assignable<T_i>::value 對 Types 中所有 T_i 為 true 。
  • (3) 不參與重載決議,除非 sizeof...(UTypes) == sizeof...(Types) 且 std::is_assignable<T_i&, const U_i&>::value 對所有 Types 中的 T_iUTypes 中的 U_i 的對應對為 true 。
  • (4) 不參與重載決議,除非 sizeof...(UTypes) == sizeof...(Types) 且 std::is_assignable<T_i&, U_i&&>::value 對所有 Types 中的 T_iUTypes 中的 U_i 的對應對為 true 。
  • (5) 不參與重載決議,除非 sizeof...(Types) == 2 且 std::is_assignable<T_0&, const U1&>::value 與 std::is_assignable<T_1&, const U2&>::value 都是 true ,其中 T_0T_1 是組成 Types 的二個類型。
  • (6) 不參與重載決議,除非 sizeof...(Types) == 2 且 std::is_assignable<T_0&, U1&&>::value 與 std::is_assignable<T_1&, U2&&>::value 都是 true ,其中 T_0T_1 是組成 Types 的二個類型。
(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)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/716268.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/716268.shtml
英文地址,請注明出處:http://en.pswp.cn/news/716268.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【AI繪畫】免費GPU Tesla A100 32G算力部署Stable Diffusion

免責聲明 在閱讀和實踐本文提供的內容之前&#xff0c;請注意以下免責聲明&#xff1a; 侵權問題: 本文提供的信息僅供學習參考&#xff0c;不用做任何商業用途&#xff0c;如造成侵權&#xff0c;請私信我&#xff0c;我會立即刪除&#xff0c;作者不對讀者因使用本文所述方法…

Matlab 機器人工具箱 RobotArm類

文章目錄 1 RobotArm1.1 方法1.2 注意2 RobotArm.RobotArm3 RobotArm.cmove4 其他官網:Robotics Toolbox - Peter Corke 1 RobotArm 串聯機械臂類 1.1 方法 方法描述plot顯示機器人的圖形表示teach驅動物理和圖形機器人mirror使用機器人作為從機來驅動圖形</

深入了解Kafka的文件存儲原理

Kafka簡介 Kafka最初由Linkedin公司開發的分布式、分區的、多副本的、多訂閱者的消息系統。它提供了類似于JMS的特性&#xff0c;但是在設計實現上完全不同&#xff0c;此外它并不是JMS規范的實現。kafka對消息保存是根據Topic進行歸類&#xff0c;發送消息者稱為Producer&…

IntelliJ IDEA 常用的插件

IntelliJ IDEA有很多常用的插件&#xff0c;這些插件可以擴展IDE的功能&#xff0c;提高開發效率。以下是一些常用的插件&#xff1a; Maven Helper&#xff1a;這是一款分析Maven依賴沖突的插件。在沒有此插件時&#xff0c;查看Maven的依賴樹和檢查依賴包沖突可能需要輸入命…

梯度下降算法(帶你 原理 實踐)

目錄 一、引言 二、梯度下降算法的原理 三、梯度下降算法的實現 四、梯度下降算法的優缺點 優點&#xff1a; 缺點&#xff1a; 五、梯度下降算法的改進策略 1 隨機梯度下降&#xff08;Stochastic Gradient Descent, SGD&#xff09; 2 批量梯度下降&#xff08;Batch…

LLM分布式訓練第一課(通訊原語)

這個系列作為TFLOPS和顯存消耗的續篇,今天開始正式連載 上一部地址: LLM 參數,顯存,Tflops? 訓練篇(5) (qq.com) 前一篇文章舉了65B模型的訓練所消耗的顯存的案例,如果把條件降低一點,我們看一下7B的模型需要多少顯存? 2byte的模型靜態參數權重(以16bit存儲) = 1…

(一)Python數據分析體系--九五小龐

課程地址&#xff1a;https://space.bilibili.com/387143299/channel/collectiondetail?sid554734 主要內容 知識體系 分析什么樣的數據 為什么使用Python做數據分析 Python近幾年的發展勢頭是有目共睹的&#xff0c;尤其是在科學計算&#xff0c;數據處理&#xff0c;A方面…

駕辰龍跨Llama持Wasm,玩轉Yi模型迎新春

今年新年很特別&#xff0c;AI工具添光彩。今天就來感受下最新的AI神器天選組合“WasmEdgeYi-34B”&#xff0c;只要短短三步&#xff0c;為這個甲辰龍年帶來一份九紫離火運的科技感。 環境準備 這次用的算力是OpenBayes提供的英偉達RTX_4090*1、24GB顯存、20核CPU、80GB內存…

產品營銷展示型wordpress外貿網站模板

工藝品wordpress外貿主題 簡約大氣的wordpress外貿主題&#xff0c;適合做工藝品進出品外貿的公司官網使用。 https://www.jianzhanpress.com/?p5377 餐飲設備wordpress外貿主題 簡潔的wordpress外貿主題&#xff0c;適合食品機械、餐飲設備公司使用。 https://www.jianzh…

Linux 開發工具vim、gcc/g++、makefile

目錄 Linux編輯器-vim 1. 基本概念 2. 基本操作 3. 正常模式命令集 4. 末行模式命令集 5. 其他操作 6. 簡單vim配置 Linux編譯器-gcc/g 1、基本概念 2、程序翻譯的過程 3. gcc如何完成程序翻譯 4、動靜態庫 Linux項目自動化構建工具-make/Makefile 1、背景 2、…

【Qt學習筆記】(四)Qt窗口

Qt窗口 1 菜單欄1.1 創建菜單欄1.2 在菜單欄中添加菜單1.3 創建菜單項1.4 在菜單項之間添加分割線1.5 給菜單項添加槽函數1.6 給菜單項添加快捷鍵 2 工具欄2.1 創建工具欄2.2 設置停靠位置2.3 設置浮動屬性2.4 設置移動屬性2.5 添加 Action 3 狀態欄3.1 狀態欄的創建3.2 在狀態…

2024最新算法:冠豪豬優化算法(CPO)求解23個基準函數

一、冠豪豬優化算法 冠豪豬優化算法(Crested Porcupine Optimizer&#xff0c;CPO)由Mohamed Abdel-Basset等人于2024年提出&#xff0c;該算法模擬冠豪豬的四種不同保護機制&#xff1a;視覺、聽覺、氣味和物理攻擊。第一和第二防御技術&#xff08;視覺和聽覺&#xff09;反…

盤點 | IT行業哪些認證含金量高

微思網絡 廈門微思網絡 作為一名IT人員&#xff0c;誰沒考幾個證 ——值得考的證書擁有的特性 ? 獲政府、企業和從業者認可&#xff1b; ? 持證人數多&#xff0c;業內共識度高&#xff1b; ? 幫持證者加分&#xff0c;快速提薪。 系統網絡方向認證 01 華為認證 華為…

設計模式學習筆記 - 設計原則 - 7.DRY 原則及提高代碼復用性

前言 DRY 原則&#xff0c;英文描述為&#xff1a; Don’t Repeat Yourself。中文直譯&#xff1a;不要重復自己。將它應用在編程中&#xff0c;可理解為&#xff1a;不要寫重讀的代碼。 可能你認為&#xff0c;這個原則很簡單。只要兩段代碼長得一樣&#xff0c;那就是違反 …

【機器學習】包裹式特征選擇之遞歸特征消除法

&#x1f388;個人主頁&#xff1a;豌豆射手^ &#x1f389;歡迎 &#x1f44d;點贊?評論?收藏 &#x1f917;收錄專欄&#xff1a;機器學習 &#x1f91d;希望本文對您有所裨益&#xff0c;如有不足之處&#xff0c;歡迎在評論區提出指正&#xff0c;讓我們共同學習、交流進…

電磁兼容(EMC):電解電容低阻如何選擇詳解

目錄 1 為何要選低阻電解電容 2 電解電容等效高頻等效電路 3 不同廠家ESR參數 4 高頻ESR特性 5 Low ESR鋁電解電容 1 為何要選低阻電解電容 在EMI超標時&#xff0c;將普通電解電容更換為低阻電解電容時&#xff0c;便通過了。這是因為低阻電解電容降低了功率回路的輻射電…

數字化轉型導師堅鵬:證券公司數字化轉型戰略、方法與案例

證券公司數字化轉型戰略、方法與案例 課程背景&#xff1a; 數字化轉型背景下&#xff0c;很多機構存在以下問題&#xff1a; 不清楚證券公司數字化轉型的發展戰略&#xff1f; 不知道證券公司數字化轉型的核心方法&#xff1f; 不知道證券公司數字化轉型的成功案例&am…

LLM 系列——BERT——論文解讀

一、概述 1、是什么 是單模態“小”語言模型&#xff0c;是一個“Bidirectional Encoder Representations fromTransformers”的縮寫&#xff0c;是一個語言預訓練模型&#xff0c;通過隨機掩蓋一些詞&#xff0c;然后預測這些被遮蓋的詞來訓練雙向語言模型&#xff08;編碼器…

【計算機網絡通信】計算機之間的局域網通信和互聯網通信方法(附Python和C#代碼)

文章目錄 前言一、局域網通信1.1 基本原理和方法1.1.1 獲取本地ip1.1.2 實現局域網內的廣播1.1.3 進行局域網通信 1.2 實現多客戶端連接1.3 Python源碼1.4 C#源碼1.5 可能存在的問題 二、互聯網通信2.1 實現原理2.1.1 內網穿透軟件2.1.2 實現互聯網通信 2.2 Python源碼2.3 C#源…

基于Java的超市商品管理系統(Vue.js+SpringBoot)

目錄 一、摘要1.1 簡介1.2 項目錄屏 二、研究內容2.1 數據中心模塊2.2 超市區域模塊2.3 超市貨架模塊2.4 商品類型模塊2.5 商品檔案模塊 三、系統設計3.1 用例圖3.2 時序圖3.3 類圖3.4 E-R圖 四、系統實現4.1 登錄4.2 注冊4.3 主頁4.4 超市區域管理4.5 超市貨架管理4.6 商品類型…