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. git错误处理

    1.今天 当我  执行  git add  somefile 的时候,出现 如下 错误: If no other git process is currently running, this prob ...

  2. Your first HTML form

    The first article in our series provides your very first experience of creating an HTML form, includ ...

  3. java sftp.exec无法执行mv命令

    编写java程序过程中,sftp上传下载建目录删除文件都可以,就是备份不行. 分析原因如下: 1.如果用的同一个用户,即sftp用户来通过 exec(ssh连接) 执行mv命令,那极有可能是在搭建sf ...

  4. centos7中没有service iptables save指令来保存防火墙规则

    1.任意运行一条iptables防火墙规则配置命令: iptables -P OUTPUT ACCEPT 2.对iptables服务进行保存: service iptables save 如果上述命令 ...

  5. ArrayList 源码解读

    ArrayList 源码解读     基于JDk 1.7.0_80 public class ArrayList<E> extends AbstractList<E> impl ...

  6. input只输入数字和小数后两位

    html:<input  name="" type="tel" value="" placeholder="请输入金额&qu ...

  7. Java语言的特点与工作原理

    Java语言的特点 1.简单性 Java语言与我们常听到的C++语言很像,但是没有C++那么繁琐.因为Java就是在C++之上设计出来的,设计者把C++的一些特性去掉了,这些特性在实际开发中,程序员也 ...

  8. kmp(循环节)

    Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...

  9. Topcoder SRM653div2

    A . 250 Problem Statement      Some people are sitting in a row. Each person came here from some cou ...

  10. 【TWRP】使用adb sideload线刷ROM的方法

    本教程详细介绍 手机刷三方ROM 之前需要安装的 TWRP 这个神器工具 楼主的手机是小米,所以此教程以小米手机为例.其他手机原理类似 第一步,解锁引导程序 访问小米的官方解锁网站并申请解锁权限. 等 ...