I've generated a vector from a relatively large CSV file and need to make objects out of each row. Problem is, there are 102 columns, so manually writing the object parameters is out of the question.
Data colNames;
for (int i = 0; i < 1; i ) {
for (int j = 0; j < content[i].size(); j ) {
string column = "col" j;
colNames.column = content[i][j];
}
}
Obviously, my syntax is wrong, but despite long google searches, I have yet to find anything that can really do this.
The object that is to be created is very simple: each column has its own value therein:
class Data
{
public:
string col0;
string col1;
string col2;
string col3;
string col4;
string col5;
string col6;
string col7;
string col8;
string col9;
string col10;
string col11;
string col12;
string col13;
string col14;
(...)
In other words, for j = 0, colNames.col0 needs to be updated, and so on.
CodePudding user response:
Have you looked at std::vector?
A row is a container of columns. The container to use is std::vector.
We'll use two structs: Data_Headers and Data_Rows:
struct Data_Headers
{
std::vector<std::string> column_headers;
};
struct Data_Rows
{
std::vector</* data type */> column_data;
};
You can access the row's data by:
Data_Type column1_data = row.column_data[0];
CodePudding user response:
I guess what you want to do is use a std::map with string keys. For example:
std::map<std::string, std::string> colNames;
for (size_t i = 0; i < 1; i ) {
for (size_t j = 0; j < content[i].size(); j ) {
std::string column = "col" std::to_string(j);
colNames[column] = content[i][j];
}
}
