我正在尝试将表达式“5 + b * 18”转换为“5+16.89*18”。我的问题在 while 循环内。我设法删除了空格。
我的代码:
double number = 16.89;
size_t found;
ostringstream converted;
string str ("5 + b * 18");
str.erase(remove(str.begin(),str.end(),' '),str.end());
found = str.find_first_of (VALID_CHARS);
while (found != string::npos)
{
converted << fixed << setprecision (8) << number;
str.insert(found, converted.str());
found = str.find_first_of (VALID_CHARS,found + 1);
}
谁能帮帮我?
我
最佳答案
insert()
会将字符串的内容移到插入文本后的右侧,但不会删除给定位置的字符。您应该使用 replace()
,指定大小为 `(仅替换单个字符)
关于c++ - 在 C++ 中加倍到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10724567/