C++ tokenise string and store in vector

2019-09-17 20:30发布

问题:

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.