系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 网络编程 > 其它综合 > 详细页面

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

时间:2020-10-06来源:www.pcxitongcheng.com作者:电脑系统城

1. 引入头文件

?
1 #include <tuple>

2. std::tuple初始化

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

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

?
1
2
3
4
5
6
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的所有要素打印出来。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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。

?
1
2
3
4
5
6
7
8
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)成单个的对象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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)。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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忽略该元素。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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:

分享到:

相关信息

  • C++ AVLTree高度平衡的二叉搜索树深入分析

    一、AVL树的概念 二、AVL树节点的定义 三、AVL树的插入 四、AVL树的旋转 1.左单旋 2.右单旋 3.左右双旋 4.右左双旋 五、进行验证 六、AVLTree的性能...

    2023-03-09

  • idea构建web项目的超级详细教程

    1、idea构建web项目 1、新建一个空项目 2、新建java模块,名为webDemo1 3、选择webDemo1右键,选择Add Framework Support 4、在WEB-INF下新建文件夹classes和lib 5、打开项目结构(Project Structure) 6、项目配置 7、模...

    2023-03-09

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载