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";
}
}