一篇搞懂C++ STL 元组std::tuple

server/2024/11/15 8:22:31/
cle class="baidu_pl">
cle_content" class="article_content clearfix">
content_views" class="markdown_views prism-atom-one-dark">cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">

class="toc">

文章目录

  • 前言
  • 什么是 `std::tuple`
  • 为什么要使用 `std::tuple`
  • `std::tuple` 的构造函数和操作函数
    • 1. 构造函数
    • 2. 操作函数
  • make_tuple函数
    • `std::make_tuple` 的功能
    • 函数原型
    • 参数
    • 返回值
    • 使用示例
    • `std::make_tuple` 的特点
  • 示例代码
  • 总结


前言

在 C++ 中࿰c;元组(Tuple)是一种用于将多个不同类型的值组合在一起的数据结构。它可以被视为一种扩展的结构体࿰c;用于在不需要创建新的类型的情况下存储多个不同类型的值。C++ 标准库提供了 <code>std::tuplecode> 作为一种灵活且强大的容器࿰c;可以在不需要定义新的类的情况下࿰c;将多个不同类型的值存储在一起。理解 <code>std::tuplecode> 的使用及其操作方式是编写灵活和高效 C++ 代码的一个重要方面。


什么是 <code>std::tuplecode>

<code>std::tuplecode> 是 C++ 标准库中的一个模板类࿰c;用于存储多个不同类型的元素。它是一种可变长度的容器࿰c;允许你将不同类型的值组合在一起࿰c;并提供了访问这些值的接口。

为什么要使用 <code>std::tuplecode>

<code>std::tuplecode> 的主要优势包括:

  • 多类型存储:可以将不同类型的数据组合在一起࿰c;而不需要创建额外的类或结构体。
  • 类型安全:<code>std::tuplecode> 提供了类型安全的访问方式࿰c;可以避免类型不匹配的问题。
  • 灵活性:支持任意数量和类型的元素࿰c;适合需要存储和操作多个不同类型数据的场景。

<code>std::tuplecode> 的构造函数和操作函数

1. 构造函数

1.1 默认构造函数

<code class="prism language-cpp">class="token comment">// 原型
stdclass="token double-colon punctuation">::tupleclass="token operator"><Typesclass="token punctuation">.class="token punctuation">.class="token punctuation">.class="token operator">> tuple_nameclass="token punctuation">;class="token comment">// 示例代码
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> tclass="token punctuation">; class="token comment">// 默认构造函数创建一个空的 tuplestdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Tuple created using default constructor\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

1.2 带参数的构造函数

<code class="prism language-cpp">class="token comment">// 原型
stdclass="token double-colon punctuation">::class="token generic-function">class="token function">tupleclass="token generic class-name">class="token operator"><Type1class="token punctuation">, Type2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token operator">>class="token punctuation">(arg1class="token punctuation">, arg2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token punctuation">)class="token comment">// 示例代码
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">tclass="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">, class="token string">"Hello"class="token punctuation">)class="token punctuation">; class="token comment">// 使用带参数的构造函数stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"First element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Second element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Third element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

1.3 复制构造函数

<code class="prism language-cpp">class="token comment">// 原型
stdclass="token double-colon punctuation">::class="token generic-function">class="token function">tupleclass="token generic class-name">class="token operator"><Type1class="token punctuation">, Type2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token operator">>class="token punctuation">(class="token keyword">const stdclass="token double-colon punctuation">::tupleclass="token operator"><Type1class="token punctuation">, Type2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token operator">>class="token operator">& otherclass="token punctuation">)class="token comment">// 示例代码
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">t1class="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">, class="token string">"Hello"class="token punctuation">)class="token punctuation">;stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">t2class="token punctuation">(t1class="token punctuation">)class="token punctuation">; class="token comment">// 使用复制构造函数stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Copied tuple: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">", " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

1.4 移动构造函数

<code class="prism language-cpp">class="token comment">// 原型
stdclass="token double-colon punctuation">::class="token generic-function">class="token function">tupleclass="token generic class-name">class="token operator"><Type1class="token punctuation">, Type2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token operator">>class="token punctuation">(stdclass="token double-colon punctuation">::tupleclass="token operator"><Type1class="token punctuation">, Type2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token operator">>class="token operator">&& otherclass="token punctuation">)class="token comment">// 示例代码
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">t1class="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">, class="token string">"Hello"class="token punctuation">)class="token punctuation">;stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">t2class="token punctuation">(stdclass="token double-colon punctuation">::class="token function">moveclass="token punctuation">(t1class="token punctuation">)class="token punctuation">)class="token punctuation">; class="token comment">// 使用移动构造函数stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Moved tuple: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">", " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

2. 操作函数

2.1 <code>std::getcode>

<code class="prism language-cpp">class="token comment">// 原型
stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><indexclass="token operator">>class="token punctuation">(tupleclass="token punctuation">)class="token punctuation">;class="token comment">// 示例代码
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">tclass="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">, class="token string">"Hello"class="token punctuation">)class="token punctuation">;class="token keyword">int i class="token operator">= stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(tclass="token punctuation">)class="token punctuation">;         class="token comment">// 获取第一个元素class="token keyword">double d class="token operator">= stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(tclass="token punctuation">)class="token punctuation">;      class="token comment">// 获取第二个元素stdclass="token double-colon punctuation">::string s class="token operator">= stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(tclass="token punctuation">)class="token punctuation">; class="token comment">// 获取第三个元素stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"i: " class="token operator"><< i class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"d: " class="token operator"><< d class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"s: " class="token operator"><< s class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

2.2 <code>std::tiecode>

<code class="prism language-cpp">class="token comment">// 原型
stdclass="token double-colon punctuation">::class="token function">tieclass="token punctuation">(var1class="token punctuation">, var2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token punctuation">) class="token operator">= tupleclass="token punctuation">;class="token comment">// 示例代码
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">tclass="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">, class="token string">"Hello"class="token punctuation">)class="token punctuation">;class="token keyword">int iclass="token punctuation">;class="token keyword">double dclass="token punctuation">;stdclass="token double-colon punctuation">::string sclass="token punctuation">;stdclass="token double-colon punctuation">::class="token function">tieclass="token punctuation">(iclass="token punctuation">, dclass="token punctuation">, sclass="token punctuation">) class="token operator">= tclass="token punctuation">; class="token comment">// 解包 tuplestdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"i: " class="token operator"><< i class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"d: " class="token operator"><< d class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"s: " class="token operator"><< s class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

2.3 <code>std::tuple_catcode>

<code class="prism language-cpp">class="token comment">// 原型
stdclass="token double-colon punctuation">::class="token function">tuple_catclass="token punctuation">(tuple1class="token punctuation">, tuple2class="token punctuation">, class="token punctuation">.class="token punctuation">.class="token punctuation">.class="token punctuation">)class="token punctuation">;class="token comment">// 示例代码
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token operator">> class="token function">t1class="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">)class="token punctuation">;stdclass="token double-colon punctuation">::tupleclass="token operator"><stdclass="token double-colon punctuation">::stringclass="token punctuation">, class="token keyword">charclass="token operator">> class="token function">t2class="token punctuation">(class="token string">"Hello"class="token punctuation">, class="token char">'A'class="token punctuation">)class="token punctuation">;class="token keyword">auto combined class="token operator">= stdclass="token double-colon punctuation">::class="token function">tuple_catclass="token punctuation">(t1class="token punctuation">, t2class="token punctuation">)class="token punctuation">; class="token comment">// 合并两个 tuplestdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Combined tuple: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">3class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

make_tuple函数

<code>std::make_tuplecode> 是 C++ 标准库提供的一个函数࿰c;用于创建 <code>std::tuplecode> 对象。它是一个便利函数࿰c;简化了 <code>std::tuplecode> 的初始化过程࿰c;特别是在你需要将多个值组合成一个 <code>std::tuplecode> 时。

<code>std::make_tuplecode> 的功能

  • 自动类型推导:<code>std::make_tuplecode> 可以根据传递给它的参数类型自动推导 <code>std::tuplecode> 的类型。这样࿰c;你不需要显式指定 <code>std::tuplecode> 的模板参数类型。
  • 简化初始化:使用 <code>std::make_tuplecode> 可以避免手动创建 <code>std::tuplecode> 的繁琐过程࿰c;简化代码的书写。

函数原型

<code class="prism language-cpp">class="token keyword">template class="token operator"><class="token keyword">typenameclass="token punctuation">.class="token punctuation">.class="token punctuation">. Typesclass="token operator">>
stdclass="token double-colon punctuation">::tupleclass="token operator"><Typesclass="token punctuation">.class="token punctuation">.class="token punctuation">.class="token operator">> class="token function">make_tupleclass="token punctuation">(Typesclass="token operator">&&class="token punctuation">.class="token punctuation">.class="token punctuation">. argsclass="token punctuation">)class="token punctuation">;
code>

参数

  • <code>Types&&... argscode>:可以传递任意数量和类型的参数࿰c;这些参数将被用来初始化 <code>std::tuplecode> 的元素。函数会根据这些参数的类型推导出 <code>std::tuplecode> 的类型。

返回值

  • 返回一个 <code>std::tuplecode> 对象࿰c;其中包含传递给 <code>std::make_tuplecode> 的参数。

使用示例

1. 基本用法

<code class="prism language-cpp">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><string>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{class="token comment">// 使用 make_tuple 创建一个 tupleclass="token keyword">auto t class="token operator">= stdclass="token double-colon punctuation">::class="token function">make_tupleclass="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">, stdclass="token double-colon punctuation">::class="token function">stringclass="token punctuation">(class="token string">"Hello"class="token punctuation">)class="token punctuation">)class="token punctuation">;class="token comment">// 访问 tuple 中的元素stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"First element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Second element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Third element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

2. 使用 <code>std::make_tuplecode> 初始化不同类型的 <code>std::tuplecode>

<code class="prism language-cpp">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{class="token comment">// 使用 make_tuple 创建一个 tupleclass="token keyword">auto t class="token operator">= stdclass="token double-colon punctuation">::class="token function">make_tupleclass="token punctuation">(class="token number">42class="token punctuation">, class="token string">"Hello World"class="token punctuation">, class="token number">3.14class="token punctuation">)class="token punctuation">;class="token comment">// 输出 tuple 的内容stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"First element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Second element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Third element: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(tclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

3. <code>std::make_tuplecode> 和类型推导

<code class="prism language-cpp">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{class="token comment">// 使用 make_tuple 创建一个 tuple 并进行类型推导class="token keyword">auto t class="token operator">= stdclass="token double-colon punctuation">::class="token function">make_tupleclass="token punctuation">(class="token number">1class="token punctuation">, class="token number">2.5class="token punctuation">, class="token char">'c'class="token punctuation">)class="token punctuation">;class="token comment">// 使用 get 函数访问元素class="token keyword">int i class="token operator">= stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(tclass="token punctuation">)class="token punctuation">;        class="token comment">// 1class="token keyword">double d class="token operator">= stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(tclass="token punctuation">)class="token punctuation">;     class="token comment">// 2.5class="token keyword">char c class="token operator">= stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(tclass="token punctuation">)class="token punctuation">;       class="token comment">// 'c'class="token comment">// 输出 tuple 的内容stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"First element: " class="token operator"><< i class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Second element: " class="token operator"><< d class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Third element: " class="token operator"><< c class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

<code>std::make_tuplecode> 的特点

  • 类型安全:<code>std::make_tuplecode> 能确保你创建的 <code>std::tuplecode> 类型与传递给它的参数类型完全匹配。
  • 简洁:简化了 <code>std::tuplecode> 的创建过程࿰c;避免了显式指定模板参数的需要。
  • 自动类型推导:能够根据参数的实际类型自动推导出 <code>std::tuplecode> 的元素类型。

示例代码

<code class="prism language-cpp">class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><tuple>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><iostream>
class="token macro property">class="token directive-hash">#class="token directive keyword">include class="token string"><string>class="token keyword">int class="token function">mainclass="token punctuation">(class="token punctuation">) class="token punctuation">{class="token comment">// 创建 tuple 实例stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">t1class="token punctuation">(class="token number">1class="token punctuation">, class="token number">3.14class="token punctuation">, class="token string">"Hello"class="token punctuation">)class="token punctuation">;class="token comment">// 使用复制构造函数stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">t2class="token punctuation">(t1class="token punctuation">)class="token punctuation">;class="token comment">// 使用移动构造函数stdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">doubleclass="token punctuation">, stdclass="token double-colon punctuation">::stringclass="token operator">> class="token function">t3class="token punctuation">(stdclass="token double-colon punctuation">::class="token function">moveclass="token punctuation">(t1class="token punctuation">)class="token punctuation">)class="token punctuation">;class="token comment">// 打印 tuple 内容stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Tuple t2: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">", " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(t2class="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Tuple t3: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(t3class="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(t3class="token punctuation">) class="token operator"><< class="token string">", " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(t3class="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token comment">// 解包 tupleclass="token keyword">int iclass="token punctuation">;class="token keyword">double dclass="token punctuation">;stdclass="token double-colon punctuation">::string sclass="token punctuation">;stdclass="token double-colon punctuation">::class="token function">tieclass="token punctuation">(iclass="token punctuation">, dclass="token punctuation">, sclass="token punctuation">) class="token operator">= t2class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Unpacked tuple t2: " class="token operator"><< i class="token operator"><< class="token string">", " class="token operator"><< d class="token operator"><< class="token string">", " class="token operator"><< s class="token operator"><< class="token string">"\n"class="token punctuation">;class="token comment">// 合并 tuplestdclass="token double-colon punctuation">::tupleclass="token operator"><class="token keyword">intclass="token punctuation">, class="token keyword">charclass="token operator">> class="token function">t4class="token punctuation">(class="token number">1class="token punctuation">, class="token char">'A'class="token punctuation">)class="token punctuation">;class="token keyword">auto combined class="token operator">= stdclass="token double-colon punctuation">::class="token function">tuple_catclass="token punctuation">(t2class="token punctuation">, t4class="token punctuation">)class="token punctuation">;stdclass="token double-colon punctuation">::cout class="token operator"><< class="token string">"Combined tuple: " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">0class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">1class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">", " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">2class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">", "class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">3class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">", " class="token operator"><< stdclass="token double-colon punctuation">::class="token generic-function">class="token function">getclass="token generic class-name">class="token operator"><class="token number">4class="token operator">>class="token punctuation">(combinedclass="token punctuation">) class="token operator"><< class="token string">"\n"class="token punctuation">;class="token keyword">return class="token number">0class="token punctuation">;
class="token punctuation">}
code>

总结

<code>std::tuplecode> 是一个强大的容器࿰c;可以将不同类型的多个值组合在一起。通过掌握其构造函数和操作函数(如 <code>std::getcode>、<code>std::tiecode> 和 <code>std::tuple_catcode>)࿰c;你可以高效地创建、操作和组合 <code>std::tuplecode> 实例。理解这些基本操作可以帮助你在复杂的数据结构处理中更加灵活地管理不同类型的数据。

class="blog-extension-box">

http://www.ppmy.cn/server/108341.html

相关文章

JS中【Data】详解

在JavaScript中&#xff0c;“Date” 是一个内置对象&#xff0c;专门用于处理日期和时间。Date 对象提供了多种方法&#xff0c;能够让你创建、操作和格式化日期和时间数据。下面我们将详细讲解 Date 对象的各个方面&#xff0c;包括它的创建、方法、使用场景和一些常见的坑。…

Java-List工具类

为了扩展 List 工具类,我们可以创建一个新的工具类 ListUtil,它将包含一系列静态方法,用于方 便地创建、操作和查询 List 集合。以下是一些常用的扩展方法示例: 批量添加元素:addAllElements 方法允许向列表中批量添加多个元素。 批量移除元素:removeAllElements 方法…

MySQL——多表操作(四)子查询(1)带 IN 关键字的子查询

子查询是指一个查询语句嵌套在另一个查询语句内部的查询。它可以嵌套在SELECT、SELECT、INTO 语句、INSERT…INTO 等语句中。在执行查询语句时&#xff0c;首会执行子查询中的语句&#xff0c;然后将返回的结果作为外层查询的过滤条件&#xff0c;在子查询中通可以使用 IN、EXI…

校园牛奶订购配送小程序开发制作方案

校园牛奶订购配送小程序系统的开发方案&#xff0c;包括对用户需求的分析、目标用户的界定、使用场景的设定以及开发功能模块的规划。校园牛奶订购配送小程序系统主要是为校园内学生和教职工提供牛奶订购与配送服务。 目标用户 主要面向在校学生、教职工以及其他有牛奶订购需求…

5G无线电链路监控(Radio Link Monitoring,简称RLM)

5G支持在无线电链路监控&#xff08;Radio Link Monitoring&#xff0c;简称RLM&#xff09;上的应用。 首先&#xff0c;我们来理解什么是RLM。在移动通信中&#xff0c;无线电链路监控&#xff08;RLM&#xff09;是一个关键功能&#xff0c;它负责监测和评估无线链路的状况。…

DeepFaceLab——面部替换和深度伪造工具

一、DeepFaceLab介绍 DeepFaceLab 是一个开源的面部替换和深度伪造工具&#xff0c;广泛用于创建逼真的面部替换视频。它利用深度学习技术&#xff0c;通过训练神经网络来合成目标面部在视频中的表现&#xff0c;从而实现面部替换。 二、DeepFaceLab的核心功能与技术实现 1.…

秋招 8.24京东笔试 JDS-2025届秋招-后端开发工程师-第3批

8.24 JDS-2025届秋招-后端开发工程师-第3批 前言 京东不同的语言(C, Java, Go)卷子是不同的&#xff0c;一开始会让你选对应的语言。 选择题分为八股文和编程逻辑题&#xff0c;编程大题有三题(15, 20, 25) 大题&#xff0c;笔者AC了前两道&#xff0c;最后一题暴力20% 希望…

VMD+皮尔逊+降噪+重构(自适应阈值筛选分量)

VMD皮尔逊降噪重构 VMD皮尔逊降噪重构代码获取戳此处代码获取戳此处代码获取戳此处代码获取戳此处 VMD&#xff08;变分模态分解&#xff09;&#xff1a; 概念&#xff1a;VMD是一种新的时频分析方法&#xff0c;能够将多分量信号一次性分解成多个单分量调幅调频信号&#xf…