C++ tuple 速记
2015-08-11 13:00
831 查看
简介
tuple 是C++11 以模板的形式开发的元组, 类似于pair , 但是支持任意的元素个数。基本的操作
int main () { /*** Construct a tuple ***/ // By constrcut std::tuple<int,char> foo (10,'x'); // By make_tuple auto bar = std::make_tuple ("test", 3.1, 14, 'y'); // By forward_as_tuple auto ref = std::forward_as_tuple(std::string("test") + "eee" , 1, 'a'); /*** Assign ***/ std::get<2>(bar) = 100; /*** Get type and get value ***/ std::tuple_element<0,decltype(bar)>::type bar_1 = std::get<0>(bar); /*** unpack a tuple ***/ int myint; char mychar; std::tie (myint, mychar) = foo; // unpack elements std::tie (std::ignore, std::ignore, myint, mychar) = bar; // unpack (with ignore) /*** get size as a type **/ std::cout << std::tuple_size<decltype(bar)>::value << "\n"; auto merge = std::tuple_cat(foo,bar); return 0; }
可以看到 , tuple的构造是比较灵活的, 可以提前声明具体实例化类型, 也可以交由编译器根据构造
参数推导。
forward_as_tuple似的tuple对右值有较好的支持。
std::tie和
std::ignore在
std::get之外提供了获取tuple内部元素的方式,且
支持批量获取。
在将一个tuple视为一种特定类型的时候 :
tuple_element提供了获取对应元素的类型的途径。
tuple_size提供了获取tuple类型大小的途径。
std::tuple_cat提供了合并tuple为一个tuple的途径
尚未有截断tuple的直接接口。
相关文章推荐
- 新写的c++日志库:log4K
- C++ 笔记(一) —— 尽量以 const、enum、inline 替换 #define
- 黑马程序员———C语言———【按位运算】
- Doxygen 注释格式
- C++输出精度控制
- C++ 何时使用动态分配(即使用new关键字)?何时使用指针?
- C++细节理解
- C/C++基本数据类型所占字节数
- lambda表达式学习
- C++ 标准输入流总结
- C语言函数之可变参数原理:va_start、va_arg及va_end
- Effective Modern C++翻译(7)-条款6:当auto推导出意外的类型时,使用显式的类型初始化语义
- [leetcode-101]Symmetric Tree(c++)
- 利用C语言来求最大连续子序列乘积的方法
- c++防止迭代器失效
- 当C++遇到IOS应用开发---LRUCache缓存
- C++单例模式
- 使用C语言提取子字符串及判断对称子字符串最大长度
- C++11 并发指南六( <atomic> 类型详解二 std::atomic )
- 再说 c++11 内存模型