简体   繁体   English

将JsonCPP ValueIterator与STL算法一起使用

[英]Using JsonCPP ValueIterator with STL algorithms

I know, ValueIterator from JsonCPP cannot be used in standard STL algorithms directly. 我知道,JsonCPP的ValueIterator不能直接在标准STL算法中使用。 But is there a some "indirect" way to use it in STL algorithms (maybe via Boost.Iterator or something like this)? 但是,是否有某种“间接”方式在STL算法中使用它(可能通过Boost.Iterator或类似的方式)? I want something likes to following: 我想要一些喜欢的东西:

Json::Value root = getJson(); //came from outside
std::vector<Json::Value> vec;

std::copy
  ( make_some_smart_iterator(...) // some iterator produced with start of root
  , make_some_smart_iterator(...) // some iterator produced with end of root
  , std::back_inserter(vec)
  );

There is a custom iterator derived from boost::iterator_facade . 有一个来自boost::iterator_facade的自定义迭代boost::iterator_facade

#include <boost/iterator/iterator_facade.hpp>

class JsonIter : public boost::iterator_facade
     <JsonIter, Json::ValueIterator, boost::forward_traversal_tag, Json::Value &>
{
public:
    JsonIter() {}
    explicit JsonIter(Json::ValueIterator iter) : m_iter(iter) {}
private:
    friend class boost::iterator_core_access;

    void increment() { ++m_iter; }
    bool equal(const JsonIter &other) const {return this->m_iter == other.m_iter;}
    Json::Value &dereference() const { return *m_iter; }
    Json::ValueIterator m_iter;
};

And client code is following: 客户代码如下:

std::copy(JsonIter(root.begin()), JsonIter(root.end()), std::back_inserter(vec));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM