在boost元组,zip_iterator等上使用std :: get和std :: tie [英] Using std::get and std::tie on boost tuples, zip_iterator, etc
问题描述
将std::get<>()
和std::tie<>()
与boost结构一起使用时,我有哪些选择?
What are my options for using std::get<>()
and std::tie<>()
together with boost constructs?
示例:
我想使用基于范围的for循环在多个容器上进行迭代.我可以实现使用boost::zip_iterator
的zip
函数.
Example:
I want to use range-based for-loop for iteration over several containers. I can implement zip
function, which uses boost::zip_iterator
.
#include <boost/iterator/zip_iterator.hpp>
#include <boost/range.hpp>
template <typename... TContainer>
auto zip(TContainer&... containers) -> boost::iterator_range<boost::zip_iterator<decltype(boost::make_tuple(std::begin(containers)...))>> {
auto zip_begin = boost::make_zip_iterator(boost::make_tuple(std::begin(containers)...));
auto zip_end = boost::make_zip_iterator(boost::make_tuple(std::end(containers)...));
return boost::make_iterator_range(zip_begin, zip_end);
}
现在我可以像这样使用它:
Now I can use it like this:
std:list<int> a;
std::vector<double> b;
...
for (auto t : zip(a, b)) {
// access elements by boost::get<0>(t), boost::get<1>(t)
// or use boost::tie(a_element, b_element)
}
当我调用其他方法(期望是std::tuple
或std::pair
-我必须转换)时会出现问题,因为其余代码使用std::tuples
,或者模板化代码使用std::get<>()
和/或std::tie()
.
Problems occur when I call some other method, which expects std::tuple
or std::pair
- I have to convert), since rest of the code uses std::tuples
, or when templated code uses std::get<>()
and/or std::tie()
.
我发现一些补丁添加了对zip_iterator
的std::tuple
支持,但是在我的版本中未应用(我使用Boost 1.54).
I found some patches which add std::tuple
support for zip_iterator
, but these are not applied in my version (I use Boost 1.54).
我错过了什么吗?强制zip_iterator
返回std::tuple
或使std::get
,std::tie
等可用于增强类型的方法是什么?
Am I missing something? What are my options to either force zip_iterator
to return std::tuple
or make std::get
, std::tie
, etc. available for boost types?
推荐答案
在增强> = 1.55中使用boost::combine
.
use boost::combine
in boost >= 1.55.
for (auto a_tuple: boost::combine(va, vb))
{...}
或
for (auto a_tuple: boost::combine(va, vb))
{...}
or
BOOST_FOREACH(boost::tie(elem_a, elem_b), boost::combine(va, vb))
{...}
BOOST_FOREACH(boost::tie(elem_a, elem_b), boost::combine(va, vb))
{...}
这篇关于在boost元组,zip_iterator等上使用std :: get和std :: tie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!