简体   繁体   中英

C++ tokenise string and store in vector

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? ie (5+5)+5

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM