C ++标记字符串并存储在向量中 [英] C++ tokenise string and store in vector

查看:85
本文介绍了C ++标记字符串并存储在向量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

    }

     return store;

}

当我输入算术表达式(5 + 5)+ 5

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

我得到输出:

(5
+
5)
+
5

(5
+
5)
+
5

但是我想要

(
5
+
5
)
+
5

(
5
+
5
)
+
5

此外,代码仅将空格之间的字符串分隔开,是否可以对没有空格的字符串进行标记化?即(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

推荐答案

您可以使用 strtok strtok_r

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

这些使您无法通过多个定界符来分割字符串.

These halp you to split your string by multiple delimiters.

如果要使用多个线程拆分字符串,请对strtok使用strtok_r.

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

如果您需要一个示例,只需在Google上搜索即可.

if you need an example, simply google it.

这篇关于C ++标记字符串并存储在向量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆