Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <initializer_list>
  3. #include <vector>
  4. #include <tuple>
  5.  
  6. struct Tag;
  7.  
  8. struct Tag {
  9.   Tag(const std::string& name, const std::string& text): name(name), text(text) {
  10.   }
  11.  
  12.   Tag(const std::string& name, std::vector<Tag> children): name(name), children(children) {
  13.   }
  14.  
  15.   std::vector<Tag> children;
  16.   std::string name;
  17.   std::string text;
  18. };
  19.  
  20.  
  21. std::ostream& operator<<(std::ostream& os, const Tag& tag) {
  22.   os << "<" << tag.name << ">" << tag.text;
  23.  
  24.   for (const auto& child: tag.children) {
  25.     os << child;
  26.   }
  27.  
  28.   os << "</" << tag.name << ">";
  29.  
  30.   return os;
  31. }
  32.  
  33. struct P: Tag {
  34.   P(const std::string& text): Tag("p", text) {};
  35.   P(std::initializer_list<Tag> tags): Tag{"p", tags} {};
  36. };
  37.  
  38. int main() {
  39.   Tag root = P {
  40.     P {
  41.       P {
  42.         "Hello"
  43.       }
  44.     }
  45.   };
  46.  
  47.   std::cout << root;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement