【问题标题】:boost::property_tree::ptree::iterator does define operator+boost::property_tree::ptree::iterator 确实定义了 operator+
【发布时间】:2018-08-31 09:27:41
【问题描述】:

std::XXX::inerator 始终支持 operator+,如

const string s = "abcdef";
cout << *(s.begin() + 3);//output d

但是ptree不支持operator+。

ptree p = root.get_child("insert");
//I want to directly goto the 3rd child.
auto iter = p.begin()+3;//error

我知道我可以使用 for 循环来执行此操作。但我想知道是否有更优雅的方式来做到这一点?

【问题讨论】:

  • 来自文档(我的重点):“Property Tree 库提供了一种数据结构,用于存储任意深度嵌套的值树,通过某个键在每个级别进行索引。树的每个节点都存储其自己的值,加上其子节点及其键的有序列表。树允许通过路径轻松访问其任何节点是多个连接的钥匙。”如果您开始将事物称为绝对或相对索引,我想您会失去相当多的灵活性。
  • 我必须这样做。

标签: boost


【解决方案1】:

我通过以下方式实现了这一目标:

template <class InputIterator, class Distance>
  void advance (InputIterator& it, Distance n);

C++ stl advance document

auto iter = p.begin();
advance(iter, 3);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-30
    • 2011-12-30
    • 2013-07-14
    • 2015-03-19
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多