C++11新特性std::tuple的使用方法

 更新时间:2020年10月06日 09:28:22   作者:半杯茶的小酒杯  
这篇文章主要介绍了C++11新特性-std::tuple的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1. 引入头文件

#include <tuple>

2. std::tuple初始化

std::tuple<int, std::string, float> t1(10, "Test", 3.14);

这里要注意,不是所有的C++ 11编译器都支持copy-list-initialization的方式。如下代码所示。

std::tuple<int, int> foo_tuple() 
{
 return {1, -1}; // Error until N4387
 return std::tuple<int, int>{1, -1}; // Always works
 return std::make_tuple(1, -1); // Always works
}

3. 打印std::tuple

打印std::tuple可以将它的元素逐个打印出来,不过非常繁琐,我们可以通过如下通用的打印函数,帮助我们一次性的将tuple的所有要素打印出来。

#include <iostream>
#include <tuple>
#include <string>
 
// helper function to print a tuple of any size
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<typename... Args, std::enable_if_t<sizeof...(Args) == 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
 std::cout << "()\n";
}
 
template<typename... Args, std::enable_if_t<sizeof...(Args) != 0, int> = 0>
void print(const std::tuple<Args...>& t)
{
 std::cout << "(";
 TuplePrinter<decltype(t), sizeof...(Args)>::print(t);
 std::cout << ")\n";
}
// end helper function
 
int main()
{
 std::tuple<int, std::string, float> t1(10, "Test", 3.14);
 print(t1);
}

输出:

(10, Test, 3.14)

4、合并多个std::tuple

std::tuple_cat函数可以将多个std::tuple合并为一个tuple。

int main()
{
 std::tuple<int, std::string, float> t1(10, "Test", 3.14);
 int n = 7;
 auto t2 = std::tuple_cat(t1, std::make_tuple("Foo", "bar"), t1, std::tie(n));
 n = 42;
 print(t2);
}

输出:

(10, Test, 3.14, Foo, bar, 10, Test, 3.14, 42)

5. std::tuple的解包(unpack)

std::tie能够将std::tuple包含的要素解包(unpack)成单个的对象。

#include <iostream>
#include <tuple>
#include <string>

int main() {
 auto info = std::make_tuple(3.8, 'A', "Lisa Simpson");
 
 double score = 0.0;
 char grade;
 std::string name;
 std::tie(score, grade, name) = info;

 std::cout << "score:" << score << ", grade:" << grade << ", name:" << name << std::endl;

return 0;
}

输出:

score:3.8, grade:A, name:Lisa Simpson

std::tie还支持std::pair对象的解包(unpack)。

#include <iostream>
#include <tuple>
#include <string>
#include <utility>

int main() {
 auto info = std::make_pair(3.8, "Lisa Simpson");
 
 double score = 0.0;
 std::string name;
 std::tie(score, name) = info;

 std::cout << "score:" << score << ", name:" << name << std::endl;

return 0;
}

输出:

score:3.8, name:Lisa Simpson

当我们不关注tuple中的某个元素时,可以使用std::ignore忽略该元素。

#include <iostream>
#include <tuple>
#include <string>
#include <utility>

int main() {
 auto info = std::make_pair(3.8, "Lisa Simpson");
 
 double score = 0.0;
 std::string name;
 std::tie(score, std::ignore) = info;

 std::cout << "score:" << score << ", name:" << name << std::endl;

return 0;
}

输出:

score:3.8, name:

参考材料

https://en.cppreference.com/w/cpp/utility/tuple/tuple_cat

到此这篇关于C++11新特性-std::tuple的使用方法的文章就介绍到这了,更多相关C++11 std::tuple内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于C中一个行压缩图的简单实现代码

    基于C中一个行压缩图的简单实现代码

    首先简单说一下什么是行压缩图,其实严格意义上应该是行压缩矩阵
    2013-05-05
  • C++二分查找在搜索引擎多文档求交的应用分析

    C++二分查找在搜索引擎多文档求交的应用分析

    这篇文章主要介绍了C++二分查找在搜索引擎多文档求交的应用,实例分析了二分查找的原理与C++的实现及应用技巧,需要的朋友可以参考下
    2015-06-06
  • C++中4种强制类型转换的区别详析

    C++中4种强制类型转换的区别详析

    这篇文章主要给大家介绍了关于C++中4种强制类型转换区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • C/C++中虚函数详解及其作用介绍

    C/C++中虚函数详解及其作用介绍

    这篇文章主要介绍了C/C++中虚函数详解及其作用介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • C语言 bind()函数案例详解

    C语言 bind()函数案例详解

    这篇文章主要介绍了C语言 bind()函数案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C++深入探究哈希表如何封装出unordered_set和unordered_map

    C++深入探究哈希表如何封装出unordered_set和unordered_map

    哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过把关键码映射的位置去寻找存放值的地方,说起来可能感觉有点复杂,我想我举个例子你就会明白了,最典型的的例子就是字典
    2022-06-06
  • C++实现职工工资管理系统课程设计

    C++实现职工工资管理系统课程设计

    这篇文章主要为大家详细介绍了C++实现职工工资管理系统课程设计,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • C语言实现旅游景点咨询系统

    C语言实现旅游景点咨询系统

    这篇文章主要为大家详细介绍了C语言实现旅游景点咨询系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-12-12
  • C++11用两个线程轮流打印整数的实现方法

    C++11用两个线程轮流打印整数的实现方法

    这篇文章主要介绍了C++11用两个线程轮流打印整数的实现方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 深入理解卡特兰数及其应用

    深入理解卡特兰数及其应用

    本篇文章是对卡特兰数及其应用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05

最新评论