what stl data structure to use
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: what stl data structure to use

Hybrid View

  1. #1
    Join Date
    Sep 2003
    Posts
    815

    what stl data structure to use

    Hello,

    I'm having diffculties to find the right data structure for me

    I have a structure with 5 fields

    lets say

    struct myStruct
    {

    int iField1;
    int iField2;
    int iField3;
    int iField4;
    string sField5;
    };

    I need some kind of data structure that will hold unknown numbers of elements of myStruct

    and I need to have each fields as a key field

    If I have only 1 field as the key
    I would have used hash_map

    for example if the key is iField1

    then I would have used hash_map with int as key and with myStruct as the data

    I need to be able to have 5 keys (and return the correct element according to 1 of them, meaning the key is not combination of them, but each one of them should be able to retrive the correct element)

    Thanks
    avi

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401
    Use vector<const myStruct> to store the data.
    Use map<int,const myStruct&> to store an index that points to the correct value of myStruct for each value of iField1.
    Use another map for each additional index.

    Adding and deleting myStructs will be some work. Updateing even more, so I recommend implementing it as delete+insert (thus the const).

  3. #3
    Join Date
    Sep 2003
    Posts
    815
    thanks for the advice!

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,471
    And, if you can, wrap the vector and maps up in another class to make the whole thing easier to use. E.g.
    Code:
    class my_container
    {
    public:
        void add(const myStruct& s)
        {
            // push_back to the vector, update the maps, etc.
        }
    
        // other functions for getting at the data
    
    private:
        std::vector<myStruct> structs_;
        std::map<int, myStruct*> map1_;
        // etc for the other maps
    };
    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


  5. #5
    Join Date
    Apr 2002
    Posts
    61
    why would this be better than building a linked list with pointers to the next index by each field ie:

    Code:
    class LInkList
    {
      public:
      //blah blah
      int add();
      int field1next();
      int filed1index(int i);
      ...
    
      private:
      int iField1;
      int iField2;
      int iField3;
      int iField4;
      string sField5;
      
      LInkList * myfield1next;
      ...
    };

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center