I am parsing a txt file in c++ and after parsing I have data in tree form like:
Movie1
Actor
N1
N2
N3
Movie2
Actor
N1
N2
Problem is how to store and pass this data to caller ?
Printable View
I am parsing a txt file in c++ and after parsing I have data in tree form like:
Movie1
Actor
N1
N2
N3
Movie2
Actor
N1
N2
Problem is how to store and pass this data to caller ?
I don't know what's a caller. Assuming it's a function, why don't you use a vector to store them, and then pass the vector as an argument of the function?
You better don´t use the tree to store your data, better use a model/document. The tree is only used to display the data. Each movie can be represented by this structure, and since you want to handle multiple movies you need a container of movies. I always use singleton documents in my applications.
Read the movie data into the document and then build the tree from the document movie entries.Code:#include <string>
#include <vector>
struct Movie
{
std::string Name;
std::vector<std::string> Actors;
};
class MovieDocument
{
std::vector<Movie> Movies_;
public:
static MovieDocument instance()
{
static MovieDocument theDocument;
return theDocument;
}
std::vector<Movie> movies()
{
return Movies_;
}
const std::vector<Movie> movies() const
{
return Movies_;
}
private:
MovieDocument()
{
}
MovieDocument( const MovieDocument& doc )
{
}
MovieDocument& operator=( const MovieDocument& doc )
{
}
};
Code:MyForm::MyForm()
{
// traverse document movie entries and build tree
for( std::vector<Movie>::const_iterator movie_it = MovieDocument::instance().movies.begin();
movie_it != MovieDocument::instance().movies.end(); ++movie_it )
{
// Insert new tree node
TreeNode* Node = RootNode->add( movie_it->Name );
// insert actors
for( std::vector<std::string>::const_iterator actor_it = movie_it->Actors.begin();
actor_it != movie_it->Actors.end(); ++actor_it )
{
Node->add( *actor_it );
}
}
}
probablyCode:static MovieDocument& instance()