How to feed Boost.PropertyTree with a string, not a file?

前端 未结 3 983
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 16:32

Boost has a tutorial on how to load XML from a file. How do I feed it with a string that I either create in code or receive from a user (e.g. with cin)?

3条回答
  •  甜味超标
    2021-01-01 17:11

    The other answers are non-ideal, because using istringstream needlessly copies the entire buffer.

    As an answer on this question suggests, you could use the deprecated istrstream, but as this generates warnings and may be removed in future, a better solution is to use boost::iostreams:

    boost::iostreams::stream stream(moo.c_str(), moo.size());
    boost::property_tree::read_json(stream, tree);
    

    This avoids needlessly copying the buffer in the same way istrstream did (which can be a considerable problem, if your input buffer is large), and saves you having to write your own stream class.

提交回复
热议问题