Hello,
my csv-file has 1000 records and every record has 15 fields separated with "/".
Now I want to analyse this data and I'm not sure how I have to store the data in memory for a fast access.
Should I store them in vector of objects or what is your advice?
What is the easiest way to read the fields of the records all at once in an structure / object....
// Example program
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
struct Record {
std::string foo;
std::string bar;
std::string bah;
};
int main()
{
// replace this with your file:
std::istringstream f("test1/patient1/1/test2/patient2/2");
//std::ifstream f("test.txt");
int num_records = 2;
std::vector<Record> records(num_records);
for (int i = 0; i < num_records; i++)
{
Record rec;
std::string token;
std::getline(f, token, '/');
rec.foo = token;
std::getline(f, token, '/');
rec.bar = token;
std::getline(f, token, '/');
rec.bah = token;
records[i] = rec;
}
for (int i = 0; i < num_records; i++)
{
std::cout << records[i].foo << " " << records[i].bar << " " << records[i].bah << '\n';
}
}
test1
patient1
1
test2
patient2
2
I'm using std::istringstream because it's easy to communicate over the internet with it. You probably want to replace std::istringstream with std::ifstream if you are reading from a file.
omg ...yes I remember ... I read it ... about heap und vectors but I forgot it.
Now I'm already working with your information and I'm happy with the first results :-)
thank you