c++ - Looking for the most elegant solution for looping a specific string -
so got dozen of strings download , 1 of example one
"australija 036 aud 1 5,136250 5,151705 5,167160"
the spaces shown single here, multiple between numbers , chars.
so first idea count manually number need (the second one, 5.151705 in example) , substring.(41,8) seems ify me.
second idea save number chars in vector, , vector[4] , save seperate variable.
and third 1 loop string until position myself after 5th group of spaces , substring it.
just looking feedback on "best".
you can think of first trimming multiple whitespace. like
std::string str = "australija 036 aud 1 5,136250 5,151705 5,167160"; str.erase(std::unique(str.begin(), str.end(), [](char a, char b) { return ::isspace(a) && ::isspace(b); }), str.end());
a string split may useful
std::vector<std::string> split(std::string & str, char delim) { std::vector<std::string> result; std::stringstream ss(str); std::string token; while (getline(ss, token, delim)) result.push_back(token); return result; }
and calling it
std::vector<std::string> result = split(str, ' ');
Comments
Post a Comment