Citat:
Ursprungligen postat av isato
Kod:
20 /*Split a string into tokenz and returns all of them */
22 vector<float> Parser::getRow(string in){
23 cout << in << endl;
24 vector<float> row;
25 string token;
26 istringstream iss(in);
27 while ( getline(iss, token, ' ') ){
28 cout << "here is token that is not added " << token << endl;
29 if(token.length() > 0){
30 cout << "adding" << token << endl;
31 row.push_back(atof(token.c_str()));
32 }
33 }
34 cout << "returning row " << endl;
35 for(int i = 0 ; i < row.size(); i++)
36 cout << row[i] << ",";
37
38 cout << endl;
39 return row;
40 }
I vissa fall fungerar den detta ger den output jag vill ha.
[PHP]192 std::string foo = "3 4 5 6 7";
193 p.getRow(foo);
[/PHP]
När jag kör den i ett större samanhang
Så skriver jag ut in(första jag gör i funktionen)
3 20 19 1
Däremot får jag bara första elementet tillbaka.
returning row
3,
Varför?
Den verkar missa att den skall splitta vid ' '
eftersom
cout << "here is token that is not added " << token << endl;
ger here is token that is not added 3 20 21 0
i det fungerande fallet ser det ut såhär
here is token that is not added 3
adding3
here is token that is not added
here is token that is not added 4
adding4
here is token that is not added
here is token that is not added
here is token that is not added 5
adding5
here is token that is not added
here is token that is not added 6
adding6
here is token that is not added
here is token that is not added 7