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)); 

http://www.cplusplus.com/reference/string/string/substr/


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -