I am new to c++ -
there may be a better way to do this but I have a CSV file with column names on top and row names along the left side. and the matrix contains double values.
i currently use a ifstream to pull in data into 3 containers -
list of column strings, list of row strings, vector of doubles.
I want to do a search on the strings in the columns to return the column position, then search the list of row strings to get the row position, then pull the value from my vector of doubles.
I then convert the vector of doubles into a 2d matrix.
Is this the best way to find the value from the table?
Also, efficiency is very important.
If it is significantly faster to convert the row strings and column strings to integers and then do the search, then that may be possible.
std::map< string, int > to map from strings to row/column indices. You then look up via:
But if "efficiency" is what you want, indexing rows and columns by name is a dumb idea. Apply a single transformation to the data set up-front which maps the strings to indices. It's kind of ridiculous to perform that lookup every single time you access the matrix.
double value = data[ rowMap[ rowName ] * colMap.size() + colMap[ colName ] ];
transform the strings into indices? is this convert the position into an integer and then use that integer wherever i would've had the string before?
Yes, but do this only once up front, not every time you access a matrix element. Of course, if the strings are being generated dynamically and handed to you, you can't do that. But if you're given a fixed data set and row/col string pairs up front, just convert everything once in the beginning.
Originally Posted by lawrenced