c++ - Splitting a line into token with find() -
i want split line :
cmd1; cmd2; cmd3
into 3 strings stock list. like
cmd1 cmd2 cmd3
so made code :
main.cpp
#include <string> #include <iostream> #include <list> int main() { std::string line("cmd1; cmd2; cmd3"); std::list<std::string> l; size_t pos = 0; size_t ex_pos = 0; while ((pos = line.find(';', ex_pos)) != std::string::npos) { l.push_back(line.substr(ex_pos, pos)); ex_pos = pos + 2; } l.push_back(line.substr(ex_pos, pos)); (std::list<std::string>::iterator = l.begin(); != l.end(); ++it) { std::cout << *it << std::endl; } return (0); }
but don't know why returns me :
cmd1 cmd2; cmd3 cmd3
second argument of substr
not index of lat character copy. length of target substring.
l.push_back(line.substr(ex_pos, pos-ex_pos));
Comments
Post a Comment