During my bachelor degree in CS I've come across the use of recursive data-structures a lot of times. In C++ I always ended up using pointers to make my data structures recursive, just like what I would do in C.
A simplified example could be the following:
struct Tree{
int data;
struct Tree *left, *right;
};
However, using pointers tends to be a risky job and involves a lot hours debugging and testing the code. For these resouns I would like to know if there is any other efficient way of defining recursive data-structures in C++.
In other programming languages, like Rust, I've seen things like that:
struct Node {
children: Vec<Node>,
node_type: NodeType,
}
Is there a safer and confortable way of defining such recursive structures in C++. One possibility would be to use std::Vector, but I am not aware of the performance of the method.
struct Tree { data: i32, left: Tree, right: Tree }. Rust'sVeccontains direction internally, it is in fact essentially identical to C++'sstd::vector. (Rust's structs and enums are values, like in C++, not pointers/references.)Box/Arc/Vecin Rust, then the equivalents in C++ (unique_ptr/shared_ptr/vector) should be just what you're looking for. (I realise I made a typo in my previous comment: "direction" should be "indirection".)