<bdo id='1nqGA'></bdo><ul id='1nqGA'></ul>
<tfoot id='1nqGA'></tfoot>
<i id='1nqGA'><tr id='1nqGA'><dt id='1nqGA'><q id='1nqGA'><span id='1nqGA'><b id='1nqGA'><form id='1nqGA'><ins id='1nqGA'></ins><ul id='1nqGA'></ul><sub id='1nqGA'></sub></form><legend id='1nqGA'></legend><bdo id='1nqGA'><pre id='1nqGA'><center id='1nqGA'></center></pre></bdo></b><th id='1nqGA'></th></span></q></dt></tr></i><div id='1nqGA'><tfoot id='1nqGA'></tfoot><dl id='1nqGA'><fieldset id='1nqGA'></fieldset></dl></div>
  • <legend id='1nqGA'><style id='1nqGA'><dir id='1nqGA'><q id='1nqGA'></q></dir></style></legend>
  • <small id='1nqGA'></small><noframes id='1nqGA'>

        解析文本以制作树数据结构

        Parsing Text to Make a Tree Data Structure(解析文本以制作树数据结构)

          <tbody id='3cGBC'></tbody>
            <bdo id='3cGBC'></bdo><ul id='3cGBC'></ul>
            • <small id='3cGBC'></small><noframes id='3cGBC'>

              <legend id='3cGBC'><style id='3cGBC'><dir id='3cGBC'><q id='3cGBC'></q></dir></style></legend>
                  <tfoot id='3cGBC'></tfoot>
                  <i id='3cGBC'><tr id='3cGBC'><dt id='3cGBC'><q id='3cGBC'><span id='3cGBC'><b id='3cGBC'><form id='3cGBC'><ins id='3cGBC'></ins><ul id='3cGBC'></ul><sub id='3cGBC'></sub></form><legend id='3cGBC'></legend><bdo id='3cGBC'><pre id='3cGBC'><center id='3cGBC'></center></pre></bdo></b><th id='3cGBC'></th></span></q></dt></tr></i><div id='3cGBC'><tfoot id='3cGBC'></tfoot><dl id='3cGBC'><fieldset id='3cGBC'></fieldset></dl></div>
                  本文介绍了解析文本以制作树数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我正在从文件中读取一行:

                  Let's say I'm reading a line from a file:

                  {Parent{{ChildA}{ChildB}}}
                  

                  更复杂的例子:

                  {Parent{{ChildA{ChildC}{ChildD}}{ChildB{ChildE}{ChildF}}}}
                  

                  用于构造树的语法是什么.

                  Which is the grammar used to construct a tree.

                  {} 括号内的任何名称都是一个节点,如果在该括号内还有其他节点(括号),则这些节点是子节点.

                  Any name inside {} brackets is a node, and if within that bracket there are other nodes (brackets), those nodes are children.

                  我能够使用计数器解析第一个特定示例,但只能找到节点的文本名称.我如何解析它以便我可以确定哪些节点是彼此的子节点?我似乎无法理解我将使用的代码.我有一种感觉,我会使用递归.

                  I'm able to parse the first specific example using a counter, but only to find the text names of the nodes. How can I parse this so that I can determine what nodes are children of one another? I can't seem to wrap my mind around the code I would use. I have a feeling I would use recursion.

                  任何帮助或建议将不胜感激.

                  Any help or advice would be appreciated.

                  C++ 是首选.

                  非常感谢.

                  推荐答案

                  如果是家庭作业,你无论如何都无法使用的答案破坏了乐趣:

                  Spoiling the fun with an answer you can't use anyway if it's homework:

                  Boost Spirit Qi 的最小实现:

                  A minimal implementation with Boost Spirit Qi:

                  #include <boost/spirit/include/qi.hpp>
                  namespace qi = boost::spirit::qi;
                  
                  typedef boost::make_recursive_variant<
                      std::vector<boost::recursive_variant_>, 
                      std::string>::type ast_t;
                  
                  void dump(const ast_t&);
                  
                  // adhoc parser rule:
                  static const qi::rule<std::string::iterator, ast_t()> node = 
                      '{' >> *node >> '}' | +~qi::char_("{}");
                  
                  int main()
                  {
                       std::string input = "{Parent{{ChildA{ChildC}{ChildD}}{ChildB{ChildE}{ChildF}}}}";
                       std::string::iterator f(input.begin()), l(input.end());
                  
                       ast_t tree;
                       if (qi::parse(f, l, node, tree))
                           dump(tree);
                       else
                           std::cerr << "Unparsed: " << std::string(f, l) << std::endl;
                  }
                  

                  很遗憾,dump 的实现几乎是等量的代码:)

                  The implementation of dump is regrettably almost the equivalent amount of code :)

                  它会打印:

                  {
                      Parent
                      {
                          {
                              ChildA
                              {
                                  ChildC
                              }
                              {
                                  ChildD
                              }
                          }
                          {
                              ChildB
                              {
                                  ChildE
                              }
                              {
                                  ChildF
                              }
                          }
                      }
                  }
                  

                  <子>这是dump(const ast_t&)的定义:

                  struct dump_visitor : boost::static_visitor<>
                  {
                      dump_visitor(int indent=0) : _indent(indent) {}
                  
                      void operator()(const std::string& s) const { print(s); }
                  
                      template <class V>
                          void operator()(const V& vec) const
                      {
                          print("{");
                          for(typename V::const_iterator it=vec.begin(); it!=vec.end(); it++)
                              boost::apply_visitor(dump_visitor(_indent+1), *it);
                          print("}");
                      }
                  
                    private:
                      template <typename T> void print(const T& v) const 
                        { std::cout << std::string(_indent*4, ' ') << v << std::endl; }
                      int _indent;
                  };
                  
                  void dump(const ast_t& tree)
                  {
                      boost::apply_visitor(dump_visitor(), tree);
                  }
                  

                  这篇关于解析文本以制作树数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                    <tbody id='KpYSm'></tbody>

                • <legend id='KpYSm'><style id='KpYSm'><dir id='KpYSm'><q id='KpYSm'></q></dir></style></legend>
                    • <bdo id='KpYSm'></bdo><ul id='KpYSm'></ul>
                            <i id='KpYSm'><tr id='KpYSm'><dt id='KpYSm'><q id='KpYSm'><span id='KpYSm'><b id='KpYSm'><form id='KpYSm'><ins id='KpYSm'></ins><ul id='KpYSm'></ul><sub id='KpYSm'></sub></form><legend id='KpYSm'></legend><bdo id='KpYSm'><pre id='KpYSm'><center id='KpYSm'></center></pre></bdo></b><th id='KpYSm'></th></span></q></dt></tr></i><div id='KpYSm'><tfoot id='KpYSm'></tfoot><dl id='KpYSm'><fieldset id='KpYSm'></fieldset></dl></div>
                            <tfoot id='KpYSm'></tfoot>
                          1. <small id='KpYSm'></small><noframes id='KpYSm'>