boost tuple

本文介绍了boost::tuple,它是std::pair的通用版本,能存储任意数量的值。文中提及用boost::tuple替代std::pair,可用boost::make_tuple()创建元组,有两种方式访问元组值,还能通过boost::tie()创建特定形式的元组tier。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值