c++ - 在没有提升的情况下拆分 c++ 字符串,而不是在空格上

标签 c++ string split

<分区>

Possible Duplicate:
Splitting a string in C++

我有一个字符串:
14332x+32x=10
我想拆分它,使其看起来像:
[14332][+32][10]
到目前为止,我已经尝试过

char c;
std::stringstream ss(equation1);
while (ss >> c) {
    std::cout << c << std::endl;
} 

但在测试打印的内容后,我认为无法根据该信息执行操作。我知道我需要在 x 和 = 上拆分字符串,但我不确定这是否可行以及是否可行。我用谷歌搜索了一下,没有找到任何有用的东西,但我也是 C++ 新手,答案可能就在我面前。
我不想使用升压。任何建议都会有所帮助!

最佳答案

考虑使用将 x= 指定为空白字符的方面:

#include <locale>
#include <iostream>
#include <sstream>

struct punct_ctype : std::ctype<char> {
  punct_ctype() : std::ctype<char>(get_table()) {}
  static mask const* get_table()
  {
    static mask rc[table_size];
    rc[' '] = std::ctype_base::space;
    rc['\n'] = std::ctype_base::space;
    rc['x'] = std::ctype_base::space;
    rc['='] = std::ctype_base::space;
    return &rc[0];
  }
};

int main() {
  std::string equation;
  while(std::getline(std::cin, equation)) {
    std::istringstream ss(equation);
    ss.imbue(std::locale(ss.getloc(), new punct_ctype));
    std::string term;
    while(ss >> term) {
      std::cout << "[" << term << "]";
    }
    std::cout << "\n";
  }
}

关于c++ - 在没有提升的情况下拆分 c++ 字符串,而不是在空格上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616840/

相关文章:

java - 如何在 Java 中格式化文件中的文本?

java - 将字符串解析为对象

c++ - 什么时候需要从函数返回引用?

java - Java 中 String.equals() 的运行时复杂度

c# - 在严格的文本中获取文本的特定部分

javascript - 正则表达式查找逗号后跟非空白字符

c++ - 元程序c++代码

C++如何识别变量的类型

c++ - 自动对比度和亮度(用于 OCR)

string - 根据语言环境将货币/浮点字符串解析为浮点类型