boost tuple

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", 4);  animal2 b("cat", 4, true);
  std::cout << a << std::endl;  std::cout << std::boolalpha << b << std::endl;
  return 0;
}

输出为:

(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", 4, true) << std::endl;
  return 0;
}

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", 4, true);
  std::cout << a.get<0>() << std::endl;
  std::cout << boost::get<0>(a) << std::endl;
  a.get<0>() = "dog";
  std::cout << std::boolalpha << a << std::endl;
  return 0;
}

输出为:

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 = 4;
  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 0;
}

输出为:

(dog 4 true)

(animal 4 true)

原文地址:https://www.cnblogs.com/sssblog/p/11057887.html

时间: 2024-08-03 15:23:31

boost tuple的相关文章

boost::tuple 深入学习讲解

#include<iostream> #include<string> #include<boost/tuple/tuple.hpp> #include<boost/tuple/tuple_io.hpp> #include <boost/tuple/tuple_comparison.hpp> using namespace std; int main(){ //boost::tuple 扩展了 C++ 的数据类型 std::pair 用以储存多个

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

1 #include <boost/tuple/tuple.hpp> 2 #include <boost/tuple/tuple_io.hpp> 3 #include <boost/tuple/tuple_comparison.hpp> 4 #include <iostream> 5 #include <string> 6 7 void TestTuple1() 8 { 9 typedef boost::tuple<std::string,

boost数据结构tuple

boost数据结构tuple tuple(元组)定义了一个有固定数目元素的容器,其中每个元素类型可以不相同,这与其它容器有着本质的区别!vector和array虽然可以容纳很多元素,但是元素的类型必须一致;tuple很有用,它是pair的泛化,可以从函数返回任意数量的值,也可以代替struct组合数据;boost.tuple使用库的方式为C++增加了这种很有用的数据结构,已被纳入C++ 11 TR1标准草案. 标准库中的pair是tuple的特例,即2-tuple(仅能持有两个成员的元组);tu

C++11和Boost库

C++11标准中引入了很多Boost库中的东西,对于所有人来说,完全可以使用C++11来替代之前使用的boost库. 但是还有一些事项需要我们注意. 发现了一篇好文,出处: https://meetingcpp.com/index.php/br/items/c11-and-boost.html Some parts of the Standard Library in C++11 are predated in boost. When playing around with C++11, you

boost::string or boost::regex

有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, local_size_z = c) in; 当中用蓝色标记出的部分(layout, local_size_x, local_size_y, local_size_z, in)为keyword,斜体字部分(a, b, c)为数据类型为unsigned int的数字,请编写一个函数,用于从文件里抽取出a, b,

C++ Boost 2 容器

C++ Boost 2 容器 在线文档:http://www.boost.org/doc/ 离线文档:解压Boost压缩包,浏览boost_1_62_0/boost_1_62_0/libs/libraries.htm#Containers 目录: Boost any 容器 引用与指针复习,引用并不会退化 boost.tuple,类似STL的pair键值对,但是tuple可以装10种不同的任意数据类型 tuple的创建,值操作要注意的 tuple.tie:创建一个所有元素类型为非const引用的t

boost::tie()和boost::variant()讲解

#include<iostream> #include<boost/tuple/tuple.hpp> #include<boost/variant.hpp> #include<boost/tuple/tuple_io.hpp> #include<boost/any.hpp> #include<vector> #include<iterator> #include<string> using namespace

Boost入门

[转载网友转载的 不过不知道原作者地址] Boost入门向导 简介:boost是一套开源的.高度可移植的C++模板库.它由C++标准委员发起,且里面很多组件有望成为下一代的C++标准库,其地位将会与STL一样.boost库的英文站点是http://www.boost.org.如果上个页面不能访问,可以看http://boost.c-view.org,它是Boost镜像.boost按功能分为:字符串.容器.算法.迭代器.数据结构.内存管理.多线程.IO等.其中字符串组中的正规表达式可以与POSIX

Linux C++学习之路(转自网络)

Module01 - Linux系统基础 由于本系列课程基于Linux(或UNIX),熟悉Linux操作系统是必要的前提. 该模块的课程包含以下方面的内容: 常用Unix/Linux命令    熟悉文件管理.文本处理.进程管理.网络.系统管理等各个方面大约100个常用的命令.    深入了解bash    了解Linux默认shell: bash 的语法.命令执行.I/O重定向.任务控制等.    正则表达式基础    由于UNIX/Linux中很多强大的文本处理命令如:grep.awk.sed