If I have the stdin
input as follows:
2014-01-23, AA, 20
2014-05-30, BB,2 //notice that I might have optional space
2015-03-24, CC, 5
//...
//... and so on
How do I write a program in C++ that efficiently parse the month and year, and also subsequent field? I am really stuck by this parsing issue.
What I want to do with the subsequent field is stored AA, 20
as a map. So map[AA]=20
so on.
I can do this myself. But I can't figure out how to read and parse it. Please help.
Attempt:
int year, month;
int num;
string key;
map<string, int> mapping;
string s;
getline(cin,s, '-');
year=stoi(s);
getline(cin,s, '-');
month=stoi(s);
getline(cin,s, ',');
//reading the AA, BB, CC field;
getline(cin,s, ',');
for (int i=0; i<s.size(); i++);
if (s[i]==' ') s.erase(i,1);
key=s;
//now, reading the number field following AA,BB, CC
getline(cin,s,'\n');
for (int i=0; i<s.size(); i++);
if (s[i]==' ') s.erase(i,1);
num=stoi(s);
mapping[key]=num;