I would like to load a comma seperaetd txt file into a map data structure from C++ on visual studio 2013 on win7.
Currently, the txt file has 5000 lines and 300 KB.
Each line is delimited by a new line.
I use getline() and It cost me 90 seconds to finish loading the whole file.
The file is like:
id , value1, value2, value3 , … // about 50+ columns
abc,36.1,69.15,96.358 , ….
pwr, ….
I need the final format in the map > data structure like: (id is an index in the map and column name is another index.)
abc value1 36.1
value2 69.15
value3 96.358
…
pwr …
My C++ code:
while (getline(file, aLine))
{
**UPDATE**
// split the line by comma
stringstream ssa(aLine);
vector<string> line;
while (ssa.good())
{
string asubStr;
getline(ssa, asubStr, ',');
line.push_back(asubStr);
}
// cast each string to double if needed.
myMap[id][valueX] = y ; // X can be 1, 2, 3, … 50,
// y is the number of a value column in
// the file,
//myMap is map <string, map<string, double >>
}
My final file size can be 60MB and 1 million lines.
Is it possible to save all data in a map in C++ ? And how to load the file very fast into C++ ? 90 secs for 5000 lines is too slow.
In C++, fgets() do not work for me because I do not know number of elements in the file.
I would like to load the file as fast as possible and then process each line in a data structure.
Thanks
More UPDATE I made a change so that only load each line as a string without doing any split.
set<string> mySet;
while (getline(file, aLine))
{
mySet.insert(aLine); // this is all what I do in the loop.
}
But, it still took 12 secs for 5000 lines. So, for 1 milion lines, it will take 40 minutes !