boost::tuple is a generalized version of std::pair. While std::pair can only store exactly two values, boost::tuple lets you choose how many values to store.

1. boost::tuple replacing std::pair

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <iostream>
#include <string> int main() {
typedef boost::tuple<std::string, int> animal;
typedef boost::tuple<std::string, int, bool> animal2;
animal a("cat", );
animal2 b("cat", 4, true);
std::cout << a << std::endl;
std::cout << std::boolalpha << b << std::endl;
return ;
}

输出为:

(cat 4)

(cat, 4, true)

2. creating tuples with boost::make_tuple()

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <iostream> int main() {
std::cout.setf(std::ios::boolalpha);
std::cout << boost::make_tuple("cat", , true) << std::endl;
return ;
}

boost::make_tuple() works like the helper function std::make_pair() for std::pair

3. reading and writing elements of a tuple

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <string>
#include <iostream> int main() {
typedef boost::tuple<std::string, int, bool> animal;
animal a = boost::make_tuple("cat", , true);
std::cout << a.get<>() << std::endl;
std::cout << boost::get<>(a) << std::endl;
a.get<>() = "dog";
std::cout << std::boolalpha << a << std::endl;
return ;
}

输出为:

cat

cat

(dog 4 true)

There are two ways to access values in a tuple. You can call the member function get(), or you can pass the tuple to the free-standing function boost::get(). In both cases, the index of the corrsponding element in the tuple must be provided as a template parameter. The member function get() and the free-standing function boost::get() both return a reference that allows you to change a value inside a tuple.

4. creating a tier with boost::tie()

Boost.Tuple supports a specific form of tuples called tier. Tiers are tuples whose elements are all reference types. They can be constructed with the function boost::tie()

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <string>
#include <iostream> int main() {
typedef boost::tuple<std::string&, int&, bool&> animal;
std::string name = "cat";
int legs = ;
bool tail = true;
animal a = boost::tie(name, legs, tail);
name = "dog";
std::cout << std::boolalpha << a << std::endl; animal b = boost::make_tuple(boost::ref(name), boost::ref(legs), boost::ref(tail));
name = "animal";
std::cout << std::boolalpha << b << std::endl;
return ;
}

输出为:

(dog 4 true)

(animal 4 true)

最新文章

  1. 创建ABPboilerplate模版项目
  2. js中的运算总结(未完待续
  3. bzoj 1791 DP
  4. web-ylbtech-数据库备份-数据库设计
  5. javaScript-原型、继承-02
  6. CSS基础深入之细说盒子模型
  7. C# Adomd Connection Analysis Service View Cube
  8. 【译】UI设计基础(UI Design Basics)--启动与停止(Starting and Stopping)(五)
  9. Make Things Move -- Javascript html5版(一)文件目录结构和工具方法准备
  10. doubango(3)--协议栈的启动过程
  11. js 检测浏览器
  12. [物理学与PDEs]第1章习题1 无限长直线的电场强度与电势
  13. 配置firewalld端口转发
  14. resful
  15. MD5加密之提取文件的MD5特征码
  16. Spring Boot + Spring Cloud 构建微服务系统(七):API服务网关(Zuul)
  17. P2163 【[SHOI2007]园丁的烦恼】
  18. iOS 实时音频采集与播放Audio Unit使用
  19. CoderForce 141C-Queue (贪心+构造)
  20. Angular 4 模板表单校验

热门文章

  1. edusoho迁移
  2. P2258子矩阵
  3. Embedding理解与代码实现
  4. Linux运维工程师前景
  5. Python算法每日一题--001--给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素
  6. Java基础复习(1)
  7. 跨平台自动构建工具v1.0.2 发布
  8. C# 身份证号码验证正则和验证函数
  9. Linux libOpenThreads库链接冲突错误
  10. 虚拟机VMware,安装中标麒麟系统,64位的,版本6.0,并安装qt