I want to be able to handle something like a table in C++. I mean table as in "excel spreadsheet" or "R dataFrame". My solution need not be that powerful, however. I do not need to add columns at runtime, but I will be adding rows to create averages. I am building this table from individual data points, and this table will be read elsewhere in the program for analysis of data. I feel like solutions such as SQLite are overkill. How can I simply represent this kind of data?
To more easily discuss options, consider the following table of ocean temperatures, which we will call data
:
| DataType | DateTime | Location | Temperature |
| ----------- | ---------------- | --------- | ----------- |
| Observation | 2020-07-03_1325 | buoy 3882 | 18.1 |
| Observation | 2020-07-03_1512 | buoy 3882 | 16.6 |
| Observation | 2020-07-03_1701 | buoy 3882 | 15.8 |
| DailyAvg | 2020-07-03_0000 | buoy 3882 | 16.8 |
It is important that I be able to access the data based on any property so that I can (somewhat) quickly gather all points from a specific location, all points with the same date, etc.
I've considered making a 2d array (something like std::vector< std::vector<boost::any> >
) but that requires the user to remember the position of the columns that hold the data they want. (e.g. for Temperature of the second data point, the user would have to use data[1][3]
). I also considered making a struct that looks something like this:
struct DataPoint {
ObservationType observationType;
DateTime dateTime;
std::string location;
double temperature;
}
And then simply using std::vector<DataPoint>
to keep track of an entire table.
Thus we come to my questions: do either of these approaches make sense? Is there another approach that makes more sense?
Secondly, in my mind a data point is immutable. Does it make sense to prevent the user from modifying the data after creation? If so, what is the best way to do this?
struct DataPoint
idea is right on the money.const std::vector<DataPoint>&
.boost::any
. Besides a struct, another potential representation is to use one vector for each column. This is for more special situations where it's more important to have column data organized sequentially in memory. Beyond these, you would be looking to much more complex data representations to meet specific needs.