C++ tokenise string and store in vector

吃可爱长大的小学妹 提交于 2019-12-12 04:58:10

问题


vector<string> ExprTree::tokenise(string expression){
     vector<string> store;
     string s;
     std::stringstream in(expression);
while(in >> s) {
  store.push_back(s);

    }

     return store;

}

When i input the arithmetic expression (5 + 5) + 5

i get the output:

(5
+
5)
+
5

However i want:

(
5
+
5
)
+
5

Also, the code only separates the strings between whitespaces, is it possible to tokenise a string that is written without whitespaces? i.e (5+5)+5


回答1:


2 Updates you can do to solve your problem:

string s;
while(in >> s)
//instead, do 

char ch;
while(in >> ch)

and then to handle the case of a blank space (and a newline?) put an if condition

if(ch != ' ')
store.push_back(ch);

That is if your desired input are just single digits. You'll have to make a much complex parser to handle bigger numbers. To make more complex parsers, this function is helpful. http://www.cplusplus.com/reference/istream/istream/peek/




回答2:


You can use strtok, strtok_r or Boost tokenizer to do what you need.

These halp you to split your string by multiple delimiters.

if you want to split string with multiple threads, use strtok_r against strtok.

if you need an example, simply google it.



来源:https://stackoverflow.com/questions/43567224/c-tokenise-string-and-store-in-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!