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", );
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的更多相关文章
- boost::tuple 深入学习解说
#include<iostream> #include<string> #include<boost/tuple/tuple.hpp> #include<bo ...
- Boost C++: 数据结构---tuple
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple_io.hpp> #include <boost/ ...
- boost数据结构tuple
boost数据结构tuple tuple(元组)定义了一个有固定数目元素的容器,其中每个元素类型可以不相同,这与其它容器有着本质的区别!vector和array虽然可以容纳很多元素,但是元素的类型必须 ...
- boost::string or boost::regex
有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...
- boost-数据类型之auto、any、tuple、variant
1.auto.decltype auto是C++11中的关键字,它可以通过类型推导自动得到变量或对象的类型,需要注意的是auto会忽略引用,因为引用其实就代表原对象: #include <v ...
- C++ 之Boost 实用工具类及简单使用
本文将介绍几个 Boost 实用工具类,包括 tuple.static_assert.pool.random 和 program_options等等.需要对标准 STL 具备一定的了解才能充分理解本文 ...
- boost::tie()和boost::variant()解说
#include<iostream> #include<boost/tuple/tuple.hpp> #include<boost/variant.hpp> #in ...
- boost 使用列子
#include <boost/lexical_cast.hpp>void test_lexical_cast(){ int number = 123; string str = &quo ...
- boost之算法
STL里的算法已经很好了,在boost里有几个小的算法 1.BOOST_FOREACH使用方法,定义一个容器里内部类型数据,容器作为参数传递. #include <iostream> #i ...
随机推荐
- HTML与CSS中的文本个人分享
文本 标题元素 注意: 在一个HTML页面中最好只使用一个<h1>标题 因为浏览器只会抓取一个多了没用 示例代码: <body> <!-- 标题元素 - <h1&g ...
- 洛谷P1378 油滴扩展(搜索)
洛谷P1378 油滴扩展 直接暴力搜索更新答案就可以了. 时间复杂度为 \(O(n!)\) . #include<stdio.h> #include<stdlib.h> #in ...
- spring4.1.8扩展实战之四:感知spring容器变化(SmartLifecycle接口)
本章是<spring4.1.8扩展实战>的第四篇,如果业务上需要在spring容器启动和关闭的时候做一些操作,可以自定义SmartLifecycle接口的实现类来扩展,本章我们通过先分析再 ...
- curl的一些常用命令
在学习nodejs中get到了一项新的技能crul curl 可以给在命令行上面给node服务器发送一些信息,然后得到服务器返回而响应信息,在命令行中打印出来. 下面是我整理的一些常用的命令:
- spring-第十二篇之两种后处理器
1.扩展IoC容器使用后处理器扩展 bean后处理器:对容器中的bean进行后处理,也就是额外的加强. 容器后处理:对IoC容器进行后处理,增强容器功能. 2.bean后处理器 负责处理容器 ...
- SR-IOV
SR-IOV 来源 http://blog.csdn.net/liushen0916/article/details/52423507 摘要: 介绍SR-IOV 的概念.使用场景.VMware 和 K ...
- Java JNA (一)—— 调用dll
Java调用C++动态链接库的方式很多,有jnative,jna等.这里介绍如何通过jna的方式调用动态链接库. 调用代码很简单,就是需要注意几个问题. 补充:如dll内部访问配置文件,需将配置文件放 ...
- elasticsearch 基础 —— Common Terms Query常用术语查询
常用术语查询 该common术语查询是一个现代的替代提高了精确度和搜索结果的召回(采取禁用词进去),在不牺牲性能的禁用词. 问题 查询中的每个术语都有成本.搜索"The brown fox& ...
- XMPP即时通讯协议使用(前传)——协议详解
XMPP详解 XMPP(eXtensible Messaging and Presence Protocol,可扩展消息处理和现场协议)是一种在两个地点间传递小型结构化数据的协议.在此基础上,XMPP ...
- 【知识强化】第三章 存储系统 3.4 主存储器与CPU的连接
我们这节课来看一下关于主存的一些知识.我们将要讲解主存的简单的模型和主存与CPU连接的连接原理. 我们之前呢在第一章已经学过了存储器的构成,包括了存储体.MAR(也就是地址寄存器).MDR(也就是数据 ...