时间:2021-05-20
1. 引入头文件
2. std::tuple初始化
这里要注意,不是所有的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 sizetemplate<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内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
c++11之后有了标准的线程库:std::thread。之前一些编译器使用C++11的编译参数是-std=c++11g++-std=c++11test.cpps
本节讨论下在C++11中怎样使用std::async来执行异步task。C++11中引入了std::async什么是std::asyncstd::async()
前面两讲《C++11并发指南二(std::thread详解)》,《C++11并发指南三(std::mutex详解)》分别介绍了std::thread和std::
在没有C++11的std::function和std::bind之前,我们使用函数指针的方式是五花八门,结构很繁琐难懂。C++11中提供了std::functi
1,简介C++11中加入了头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准