CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 1999
    Location
    Israel
    Posts
    49

    storing paralel data - BEST way

    Hi,
    I need to store a list of names [surname, first, title].
    What objects are best for saving this data ?
    1. list per each component [array for surname, first, title ].
    2. array of structures of Names [struct including surname, first, title]
    3. STL way.
    4. ELSE?

    Thanks!

  2. #2
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Either (2) or (3). Depends on how you wish to initialize it, access it and how fast the access should be. Even with (3), there are a whole bunch of containers. The one you choose depends on how you wish to access the data more than anything else.
    Succinct is verbose for terse

  3. #3
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    A fourth way would be to store it in a database. This in case you have a lot of data and need to be able to do advanced things with it. Like looking for all people who have the same title, while also being able to quickly look up people's title based on surname and first name.

    BTW, I would include something like a unique ID for a person, since there might be two people with the same name and title.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  4. #4
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    2 and 3 would be best:

    Code:
    struct Name
    {
         Name() {}
         Name(std::string f, std::string m, std::string l)
           : First(f), Middle(m), Last(l) {}
    
         std::string First;
         std::string Middle;
         std::string Last;
    };
    
    // typedefs for simplification
    typedef std::vector<Name> NameList;
    typedef NameList::iterator NameListIter;
    typedef NameList::const_iterator NameListConstIter;
    
    int main()
    {
         NameList n;
         n.push_back(Name("John", "H.", "Doe"));
    
         for( NameListIter iter = n.begin(); iter != n.end(); ++iter )
         {
              Name& name = *iter;
              // do what you need
         }
    
         return 1;
    }
    Using a standard container gives you access to many algorithms that would be useful, such as std::find(). typedefing the container is useful because it 1) simplifies the code, and 2) gives you one place to change if you need to change the container type.

    Jeff

  5. #5
    Join Date
    Nov 1999
    Location
    Israel
    Posts
    49
    Hi Jfaust ,
    Thanks for your code, and for advise.
    I started to use STL list container, and made a class inherited publically from it.

    I. it seems to me there are little differences between vector & list container, regarding algorithms, do you have any idea?

    II. How is CArray of CString instead of STL? Better/worse

    Thanks anyway.
    Dalit

  6. #6
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    1. Well, the big advantage of std::vector over std::list is that a vector has guaranteed constant time random access to elements. It also guarantees contiguous storage of elements in case you might need that.

    2. It has to be worse It's only implemented for MFC, so If you ever decide to reuse code for a) a project that doesn't use MFC (such as ATL, or Win32 API, or console) or b) a different platform / compiler, then CString and CArray won't be available.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  7. #7
    Join Date
    Nov 1999
    Location
    Israel
    Posts
    49

    Thumbs up

    Hi Jeff,

    Thanks.
    I will take your views into concideration & into projects.
    Bye,
    Dalit

  8. #8
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Originally posted by dalitg
    Hi Jeff,

    Thanks.
    I will take your views into concideration & into projects.
    Bye,
    Dalit
    Is everybody here called Jeff, or were you replying to Jeff a second time ?

  9. #9
    Join Date
    Nov 1999
    Location
    Israel
    Posts
    49
    Ok Yves, i admit noticing that you replied, and not Jfaust.
    so,.
    Bye,
    P.S. Yves , nice footer- I will let you know what does it mean.
    Dalit

  10. #10
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Originally posted by dalitg
    Hi Jfaust ,
    Thanks for your code, and for advise.
    I started to use STL list container, and made a class inherited publically from it.

    I. it seems to me there are little differences between vector & list container, regarding algorithms, do you have any idea?

    II. How is CArray of CString instead of STL? Better/worse

    Thanks anyway.
    Dalit
    Don't inherit publically from STL containers - they're not designed for it.

    There are massive differences between vector and list. Get hold of Effective STL by Scott Meyers for the full story. One major difference is that you (generally) can't pass a list to any of the algorithms - that's why list has member functions to do the same job.

    CArray and CString don't even come close, IMHO.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


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