how do I store and retrieve this data in my class?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: how do I store and retrieve this data in my class?

  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    254

    how do I store and retrieve this data in my class?

    Hello

    I have a class I use to store column data read from delimited text files.

    Code:
    class column_data {
    
    // class functions here
    
    public:
    
      // initialize class members
       column_data()
          : header(""),
            type(""),
            use(""),
            validate_r2(0.0),
            validate_mae(0.0),
            validate_dae(0.0),
            validate_se(0.0)  { }
    
       string header, type, use;
       double validate_r2, validate_mae, validate_dae, validate_se;
       vector<int> int_data;
       vector<string> input_string_data;
       vector<double> double_data, transformed_double_data;
    
    };
    Each column is read into the vector of string. Next the string data is copy converted to the vector of double or the vector of int depending on the data type. There can also be additional transformations of the data (normalization, scaling, etc) and the transformed data is copied to the transformed_double_data vector. I do end up with multiple copies of the data, but I can clear some of the vectors if I don't need all the versions of the data anymore. Each column object goes into a container like a vector of objects or map of objects depending on how I need to access the column data.

    There are functions that calculate statistics on the data in the columns and the results are assigned to the object variables like validate_r2. There are separate statistics for each column, so it seems logical to store the results in the column object. This works well for statistics like validate_r2 (pearson's correlation coefficient).

    The current issue is that I have other statistics like "pw50" that I need to calculate and store. The statistic is the percent within +/- 50 units and the request for pw50 is passed in from the command line. The issue is that there could be several of these and they can have more or less any value like, pw50, pw75, pw100, etc. Storing data like this doesn't fit into my scheme very well because I can't pre-declare a variable to hold the result for every possible option. I don't know in advance how many, if any, of these there will be. I was thinking of adding two vectors, one of string and one of double,

    Code:
    class column_data {
    
    public:
    
      // initialize class members
       column_data()
          : header(""),
            type(""),
            use(""),
            validate_r2(0.0),
            validate_mae(0.0),
            validate_dae(0.0),
            validate_se(0.0)  { }
    
       string header, type, use;
       double validate_r2, validate_mae, validate_dae, validate_se;
       vector<int> int_data;
       vector<string> input_string_data;
       vector<double> double_data, transformed_double_data;
       vector<double> pw_data_values;
       vector<string> pw_data_labels;
    
    };
    For each of this type of statistic requested, I would push_back a label into the string vector and then put the result into the parallel position in the double vector. This seems like I would work, but there are probably some things that could be done with pointers that might be better. Another option would be a vector of objects where each object had a string value for the label and a double for the result. That would be more bullet proof as far as keeping the label and value together, but I have not ever nested objects in a class like that so I thought I would post. I don't know the syntax for declaring an object of class_b inside the definition of class_a. I don't know if it's even a good idea to do that kind of thing. The need is to provide dynamic storage for an unspecified number of these statistics and a reliable way to retrieve the values using the label.

    Of course if you have comments, criticisms, or suggestions about any of the rest of what I am already doing above, that would be very welcome as well.

    Thanks for the suggestions,

    LMHmedchem

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,297

    Re: how do I store and retrieve this data in my class?

    I didn't go into full details of your post, but it sounds like you are tyring to use some kind of dictionary-like data structure, that would map, say a threshold (50 for pw50), which would map to the actual values you want stored?

    Lookup std::map, or, if you have C++1 std::unordered_map.
    http://www.cplusplus.com/reference/map/map/
    http://www.cplusplus.com/reference/u...unordered_map/

    Then you just store:

    Code:
    map<double, vector<double> > pwTables;
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center