|
-
March 24th, 2009, 08:13 AM
#1
How to pass data in tree form to caller ?
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 ?
-
March 24th, 2009, 08:23 AM
#2
Re: How to pass data in tree form 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?
-
March 24th, 2009, 08:31 AM
#3
Re: How to pass data in tree form to caller ?
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.
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 )
{
}
};
Read the movie data into the document and then build the tree from the document movie entries.
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 );
}
}
}
Last edited by GNiewerth; March 24th, 2009 at 08:40 AM.
Reason: More source code added
- Guido
-
March 24th, 2009, 11:18 AM
#4
Re: How to pass data in tree form to caller ?
probably
Code:
static MovieDocument& instance()
-
March 25th, 2009, 03:26 AM
#5
Re: How to pass data in tree form to caller ?
 Originally Posted by yzaykov
probably
Code:
static MovieDocument& instance()
Most probably... almost certainly
Last edited by GNiewerth; March 25th, 2009 at 03:32 AM.
- Guido
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|