0

I think the title it's pretty explanatory, anyways, here's what I'm trying to do: given an input of a string of integers between 0 and 9, every number separated with a comma, convert it into a std::vector. I already know the lenght of the string so the vector is already resized properly

I know it has been answered before, (How can I read and parse CSV files in C++?) but I'm looking for a solution without external libraries (it's for an assignement).

1) this version works but I think it's very rudimental and I'm trying to improve it

string line;
vector<int> numers;
int i=0;
...
for (string::iterator it=line.begin(); it<line.end(); it=it+2){ //I need to skip commas
    numbers[i]=*it-48;    

2) this is what I think would perform better, but can't get it to work!

line.erase(std::remove(line.begin(), line.end(), ','), line.end());
transform(line.begin(),line.end(),numbers.begin(),[](const string &str){return stoi(str);});

this solution doesn't compile

Any better idea?

Community
  • 1
  • 1
mariob6
  • 469
  • 1
  • 6
  • 16

0 Answers0