-
June 30th, 2014, 04:14 AM
#1
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?
-
June 30th, 2014, 04:33 AM
#2
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
-
June 30th, 2014, 08:27 PM
#3
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.
-
June 30th, 2014, 11:36 PM
#4
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).
-
June 30th, 2014, 11:55 PM
#5
Re: A meaningful way of accessing a table by container
Originally Posted by mce
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|