boost :: make_zip_iterator的decltype?

时间:2013-07-31 22:38:40

标签: c++ boost c++11

我有以下代码:

std::vector<PriceQuote, tbb::scalable_allocator<PriceQuote> > cBids(maxSize);
std::vector<PriceQuote, tbb::scalable_allocator<PriceQuote> > cAsks(maxSize);

auto zipBidsAsks = boost::make_zip_iterator(boost::make_tuple(cBids.begin(), cAsks.begin()));

如果我想decltype返回值,那么我可以将其存储在auto的{​​{1}}个decltype中,而不是将其存储在boost::make_zip_iterator中。那段代码是什么样的?

我试过了:

typedef decltype(boost::make_zip_iterator(std::vector<PriceQuote>, std::vector<PriceQuote>)) zipper_type;

// type referred to by zipper_type::iterator
typedef std::iterator_traits<zipper_type::iterator>::value_type zipped_type;

 zipped_type zipBidsAsks = boost::make_zip_iterator(boost::make_tuple(cBids.begin(), cAsks.begin()));

但这甚至没有接近工作。最后,如果我想迭代zipBidsAsks并获取每个<0><1>。怎么做的?

访问代码现在出错:

    struct PriceBookEventData
{
    timeval ts;
    unsigned totalSize;
    unsigned maxSize;


    typedef decltype
    (
         boost::make_zip_iterator(boost::tuple<std::vector<PriceQuote>::iterator,
                                               std::vector<PriceQuote>::iterator>())
    ) zipper_type;

     zipper_type zipBidsAsks;
};

void AGUI::HandlePriceBookChange(const PriceBookEventData pbed)
{
int k = 0;

while(0 != stop--)
{
    PriceQuote pqb = boost::get<0>(pbed.zipBidsAsks[k]);
    PriceQuote pqa = boost::get<1>(pbed.zipBidsAsks[k]);


/data/cbworkspace/AGUI/AGUI.cpp|101|error: no matching function for call to ‘get(boost::detail::operator_brackets_result<boost::zip_iterator<boost::tuples::tuple<__gnu_cxx::__normal_iterator<PriceQuote*, std::vector<PriceQuote> >, __gnu_cxx::__normal_iterator<PriceQuote*, std::vector<PriceQuote> > > >, boost::tuples::cons<PriceQuote&, boost::tuples::cons<PriceQuote&, boost::tuples::null_type> >, boost::tuples::cons<PriceQuote&, boost::tuples::cons<PriceQuote&, boost::tuples::null_type> > >::type)’|

1 个答案:

答案 0 :(得分:3)

我不确定为什么要使用decltype而不是auto来确定类型,后者是专门为此类案例设计的。使用decltype代替繁琐。

你接近你所尝试的内容,除了你给了boost::make_zip_iterator一对向量,而不是tuple向量处理器。

试试这个

typedef decltype(
  boost::make_zip_iterator(
    boost::tuple<
      std::vector<PriceQuote>::iterator, 
      std::vector<PriceQuote>::iterator>()
  ) 
) zipper_type;

至于迭代zip迭代器,这是一个简单的例子:

#include <iostream>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <vector>

int main()
{
    std::vector<int> v1{1,2,3,4}, v2{10,20,30,40};

    std::for_each(
        boost::make_zip_iterator(boost::make_tuple(v1.begin(), v2.begin())),
        boost::make_zip_iterator(boost::make_tuple(v1.end(), v2.end())),
        []( boost::tuple<int, int> const& tup ) {
            std::cout 
              << boost::get<0>(tup) 
              << ", " 
              << boost::get<1>(tup) 
              << std::endl;
        }
    );
}

输出:

1, 10
2, 20
3, 30
4, 40