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)

boost tuple的更多相关文章

  1. boost::tuple 深入学习解说

    #include<iostream> #include<string> #include<boost/tuple/tuple.hpp> #include<bo ...

  2. Boost C++: 数据结构---tuple

    #include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <boost/ ...

  3. boost数据结构tuple

    boost数据结构tuple tuple(元组)定义了一个有固定数目元素的容器,其中每个元素类型可以不相同,这与其它容器有着本质的区别!vector和array虽然可以容纳很多元素,但是元素的类型必须 ...

  4. boost::string or boost::regex

    有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...

  5. boost-数据类型之auto、any、tuple、variant

    1.auto.decltype   auto是C++11中的关键字,它可以通过类型推导自动得到变量或对象的类型,需要注意的是auto会忽略引用,因为引用其实就代表原对象: #include <v ...

  6. C++ 之Boost 实用工具类及简单使用

    本文将介绍几个 Boost 实用工具类,包括 tuple.static_assert.pool.random 和 program_options等等.需要对标准 STL 具备一定的了解才能充分理解本文 ...

  7. boost::tie()和boost::variant()解说

    #include<iostream> #include<boost/tuple/tuple.hpp> #include<boost/variant.hpp> #in ...

  8. boost 使用列子

    #include <boost/lexical_cast.hpp>void test_lexical_cast(){ int number = 123; string str = &quo ...

  9. boost之算法

    STL里的算法已经很好了,在boost里有几个小的算法 1.BOOST_FOREACH使用方法,定义一个容器里内部类型数据,容器作为参数传递. #include <iostream> #i ...

随机推荐

  1. JS各种变量是true或者false列表

    如果操作数是一个对象,返回true 如果操作数是一个空字符串,返回false如果操作数是一个非空字符串,返回true如果操作数是数值0,返回false如果操作数是任意非0数值(包括Infinity), ...

  2. 125、TensorFlow计算图的执行

    # TensorFlow使用tf.Session类来表示客户端程序之间的链接 # 虽然一个在其他语言中相似的接口也是可以使用的,列如C++ runtime # 一个tf.Session对象提供了访问本 ...

  3. day17—Flex弹性布局详解(一)

    转行学开发,代码100天——2018-04-02 今天看到一篇大神的文章,关于flex布局的详解,对flex用法介绍的相当详细,非常有助于我等初学者更深入了解这种布局方式. 文章链接 [基础知识]Fl ...

  4. Eclipse报内存溢出

    (1)在配置tomcat的JDK里面设置.Window-->proference->Myeclipse-->servers-->Tomcat5-->JDK里面设置: -X ...

  5. Flannel - 配置

    原文地址 flannel 从 ETCD 中读取配置. 默认情况下,flannel 从 /coreos.com/network/config 中读取配置,可以使用 --etcd-prefix 覆盖. 通 ...

  6. HTML --JS 选择框

    <html> <head> <title>选择框</title> <script language="JavaScript"& ...

  7. HTML--JS 获取选择框信息

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. StringBuilder -字符串缓冲区,节约内层空间变长数组

    package cn.learn; /* 字符串缓冲区 -缓冲可提高效率 java.lang.StringBuilder 字符串的底层是一个被final修饰的数组,不能改变,是一个常量 private ...

  9. Python 学习笔记18 异常处理

    我们在编码的过程中,难免会遇到一些错误和异常, 这时候程序会异常退出,并且会抛出错误信息: 比如: print(1/0) ''' 输出: Traceback (most recent call las ...

  10. CentOS利用Lua访问Redis

    首先确保你编译的Lua是支持链接外部动态链接库的.因为在对Redis进行访问时是需要使用socket通信的, 而这依赖于外部的C语言写的动态连接库. 首先,这里先下载Redis的Lua客户端访问包re ...