服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++11新特性std::tuple的使用方法

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

2021-09-28 11:00半杯茶的小酒杯 C/C++

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

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:

参考材料

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

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

原文链接:http://www.banbeichadexiaojiubei.com/index.php/2020/10/05/c11%E6%96%B0%E7%89%B9%E6%80%A7-stdtuple/

延伸 · 阅读

精彩推荐