CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Dec 2003
    Posts
    244

    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 ?

  2. #2
    Join Date
    Mar 2009
    Location
    Granada, Spain
    Posts
    40

    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?

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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

  4. #4
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: How to pass data in tree form to caller ?

    probably
    Code:
    static MovieDocument& instance()

  5. #5
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: How to pass data in tree form to caller ?

    Quote Originally Posted by yzaykov View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured