C++ 如何使用istream_迭代器拆分方程?

C++ 如何使用istream_迭代器拆分方程?,c++,istream-iterator,C++,Istream Iterator,我试图将一个字符串(如(1+2)拆分为一个向量,当使用istream\u迭代器时,它不会拆分括号,因此我得到向量输出,如 (1,+,2)当我想要(,1,+,2,) 是否可以使用istream\u迭代器s来实现这一点 string eq = "(1 + 2)"; istringstream ss(eq); istream_iterator<string> begin(ss); istream_iterator<string> end; vector<string&g

我试图将一个字符串(如
(1+2)
拆分为一个向量,当使用
istream\u迭代器时,它不会拆分括号,因此我得到向量输出,如

(1,+,2)
当我想要
(,1,+,2,)

是否可以使用
istream\u迭代器
s来实现这一点

string eq = "(1 + 2)";

istringstream ss(eq);
istream_iterator<string> begin(ss);
istream_iterator<string> end;
vector<string> vec(begin, end);
string eq=“(1+2)”;
istringstream ss(eq);
istream_迭代器开始(ss);
istream_迭代器端;
向量向量向量(开始、结束);

我认为您无法使用
istream\u迭代器来完成这项工作。相反,只需手工操作即可:

vector<string> vec;
vec.reserve(eq.size() / 4); // rough guess
bool in_number = false;
for (char ch : eq) {
    if (isspace(ch)) {
        in_number = false;
    } else if (isdigit(ch)) {
        if (in_number) {
            vec.back().push_back(ch);
        } else {
            vec.emplace_back(1, ch);
            in_number = true;
        }
    } else {
        vec.emplace_back(1, ch);
        in_number = false;
    }
}
vec;
向量保留(等式大小()/4);//粗略猜测
bool in_number=false;
for(字符ch:eq){
if(isspace(ch)){
in_数=假;
}否则,如果(isdigit(ch)){
如果(单位编号){
向后推(ch);
}否则{
向量后置(1,ch);
in_数=真;
}
}否则{
向量后置(1,ch);
in_数=假;
}
}

您可以通过创建自定义类型
标记并将其用于
istream\u迭代器
。额外功能:此代码将解析多个数字、多个运算符和嵌套表达式。所以享受吧。:)

#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类令牌{
私人:
字符串val;
公众:
标记():val(“”{}
令牌(string&v):val(v){}
friend istream&operator>>(istream&in、Token&tok);
friend ostream&operator(istream&in、Token&tok){
字符c;
字符串v;
如果(在>>c中){
if(isdigit(c)){
v、 推回(c);
while(在>>c&&isdigit(c)中){
v、 推回(c);
}
收回(c);
}else if(c==“”){
而(在>>c&&c=='')中;
收回(c);
}否则{
v、 推回(c);
}
}
tok=v;
返回;
}

ostream&operator show us your code.sry第一次发布时,我刚刚添加了它,但因为它是一个向量,所以不能将“char”推回“vector”
#include <iterator>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <cctype>

using namespace std;

class Token {
private:
  string val;
public:
  Token() : val("") {}
  Token(string& v) : val(v) {}
  friend istream& operator>>(istream &in, Token& tok);
  friend ostream& operator<<(ostream &out, Token& tok);
};

istream& operator>>(istream &in, Token& tok) {
  char c;
  string v;
  if (in >> c) {
    if (isdigit(c)) {
      v.push_back(c);
      while (in >> c && isdigit(c)) {
    v.push_back(c);
      }
      in.putback(c);
    } else if (c == ' ') {
      while (in >> c && c == ' ') ;
      in.putback(c);
    } else {
      v.push_back(c);
    }
  }
  tok = v;
  return in;
}

ostream& operator<<(ostream &out, Token& tok) {
  out << tok.val;
  return out;
}

int main() {
  string eq = "(1 + 2)";
  //eq = "(100 + 200)"; // multiple digits
  //eq = "(100 + 200 * 300)"; // extra operator
  //eq = "(100 + (200 * 300))"; // nested parens

  istringstream ss(eq);
  istream_iterator<Token> begin(ss);
  istream_iterator<Token> end;
  vector<Token> vec(begin, end);
  for (auto& x : vec) {
    cout << "[" <<  x << "] ";
  }
  cout << endl;
}