Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <iostream>
- #include <string>
- #include <variant>
- #include <vector>
- using namespace std::literals;
- class Node {
- using Value = std::variant<nullptr_t, std::string, std::vector<Node>>;
- Value value_;
- public:
- Node() = default;
- template <typename T>
- Node(T value)
- : value_(std::move(value))
- {
- }
- Value& GetValue() { return value_; }
- };
- int main()
- {
- Node root;
- std::vector<Node*> stack_ { &root };
- // StartArray
- auto& curr_value = stack_.back()->GetValue();
- assert(std::holds_alternative<nullptr_t>(curr_value));
- curr_value = std::move(std::vector<Node> {});
- // Add value to array on stack
- auto& new_node = stack_.back()->GetValue();
- assert(std::holds_alternative<std::vector<Node>>(new_node));
- auto& node = std::get<std::vector<Node>>(new_node).emplace_back("simple_string");
- // the top of the stack is still an array
- auto new_back = stack_.back()->GetValue();
- assert(std::holds_alternative<std::vector<Node>>(new_back));
- // end array
- stack_.pop_back();
- auto& value = root.GetValue();
- assert(std::holds_alternative<std::vector<Node>>(value));
- std::cout << std::get<std::vector<Node>>(value).size() << '\n';
- auto simple_string = std::get<std::vector<Node>>(value).front();
- std::cout << std::get<std::string>(simple_string.GetValue());
- }
Advertisement
Add Comment
Please, Sign In to add comment