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 ...
随机推荐
- ECSHOP2.7源码分析
目录结构
- 【运维】使用FileZilla搭建FTP服务器
一.下载Filezilla Server 官网网址:https://filezilla-project.org 二.安装Filezilla Server Filezilla Server的安 ...
- codeforces 380A Sereja and Prefixes (递归)
题目: A. Sereja and Prefixes time limit per test 1 second memory limit per test 256 megabytes input st ...
- Windows7系统C盘空间不足
C盘要满了,用WizTree发现:;两个大文件, ①睡眠有关的,用命令提示符(管理员身份运行), 命令窗口中输入 powercfg -h off,即可关闭休眠功能,同时 Hiberfil.sys 文件 ...
- Ehcache配置项及持久化到硬盘
EhCache 常见的配置项: cache元素的属性 name:缓存名称 maxElementsInMemory:内存中最大缓存对象数 maxElementsOnDisk:硬盘中最大缓存对象数,若是0 ...
- VUEJS(vuejs) 数组数据不及时刷新
在Vue对象中的methods属性中构建一个方法用于刷新data使用Vue.set方法进行手动刷新methods:{update:function(o){Vue.set(this,'list',o); ...
- 终端参数上报后,平台通过tcp协议接收到相应数据并处理。
终端将终端参数以json格式的数据发送至平台.终端上电后上报,可以不认证直接上报. 实现流程如下. 1.设置终端参数上报的协议类型,例如:0x0000. public static final int ...
- HNOI2019fish
\({\rm fish}\) 20分: 六个for,点积判锐角钝角. #include <vector> #include <queue> #include <cmath ...
- kmp(前中后最长相同长度)
http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) ...
- php提交表单时如何保留多个空格及换行的文本样式
需求是:用户提交表单时屏蔽敏感词的功能.其中敏感词来自服务器端同一路径下的ciku.txt,敏感词通过"|"连接,例如"g|c|a",提交表单时替换敏感词,更重 ...