3

我有以下代码:

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返回值,而不是将其存储在 an 中,auto我可以将它存储在decltype任何boost::make_zip_iterator返回的 a 中。该代码是什么样的?

我努力了:

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)’|
4

1 回答 1

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
于 2013-07-31T23:00:58.887 回答