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

    A meaningful way of accessing a table by container

    Hi

    I need a "meaningful" way of accessing a table, the column is representing Err magnitude, and the row is representing Rate magnitude. For each error magnitude and rate magnitude, i define an action magnitude, which is the contains of the table.
    Fr example,

    Code:
    int matrix[10][10];
    
    int Action1 = matrix[0][0];
    int Action2 = matrix[0][1];
    However, i need a better way of getting matrix[0][0], row and col itself is meaningless. I want to access the table like

    "Action magnitude" = matrix["Rate magnitude 1"]["Err magnitude 2"]; using a string instead of int id. How to easily do this in c++ container?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: A meaningful way of accessing a table by container

    Didn't you consider to use enum?
    Code:
    enum Rate
    {
       RateMagnitude1 = 0,
       RateMagnitude2,
       RateMagnitude3,
       ...
    };
    enum Err
    {
       ErrMagnitude1 = 0,
       ErrMagnitude2,
       ErrMagnitude3,
       ...
    };
    ...
    //  the same - for actions...
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2002
    Posts
    788

    Re: A meaningful way of accessing a table by container

    Thanks VictorN, i haven't thought of that.
    But the number of defined Err and Rate magnitude could be from 0 to N, where N is determined in run time... is there an expandable enum or something similar?

    Also, if i were to let the user defined the meaningful terms to use, such as instead of calling Err Magnitude, user may choose to call it as Err direction1, Err direction2 etc.. and then access the table based on this meaningful name, using Enum may not be able to achieve this.. I would like something like QMapped table access..
    Last edited by mce; June 30th, 2014 at 08:36 PM.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: A meaningful way of accessing a table by container

    It sounds like you can use a std::map of std::map objects (or std::unordered_map).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: A meaningful way of accessing a table by container

    Quote Originally Posted by mce View Post
    I would like something like QMapped table access..
    If you want to use containers efficiently get the C++ Standard Library by Josuttis, second edition.

    You'll find direct replacements for QMap there. You could also play it simple and store the symbols and associated indexes as structs in an ordinary array (or an std::array or an std::vector) and locate them by making a linear search. It works fine for small data sets.

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