c++ - 在 C++ 中加倍到字符串

标签 c++ string double

我正在尝试将表达式“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/

相关文章:

c++ - 如何从 AM_MEDIA_TYPE 中获取 MEDIASUBTYPE 的名称,c++

c++ - 在 C++ 中验证 DO-178B/C 合规性

javascript - 如何实现parseFloat

PHP:如何检查字符串是否不是数字,但可以包含空格?

c# - 使用 C# 将代码(以类 C 或类 Lisp(或任何)语言编写)解析为数组的好方法?

c++ - 初始化 COM 对象/将 VB 代码转换为 C++

c++ - 如何从 stringstream 中提取 double

java - 只是字符串中的数字

c - 为什么将某个short 转换为double 等于4004 位数字?

c++ - 使用 XAudio2 加载 WAV 文件