【发布时间】:2020-11-22 17:19:45
【问题描述】:
我在一个字符串中有两个单独的数字。我想出了如何用分隔符分割字符串(输出为 136),但我坚持如何将这两个数字相乘并将结果存储在一个变量中。有什么建议吗?
#include <iostream>
#include <string>
int main()
{
std::string b = "13,6";
std::string delimiter = ",";
size_t pos = 0;
std::string token;
while ((pos = b.find(delimiter)) != std::string::npos) {
token = b.substr(0, pos);
std::cout << token;
b.erase(0, pos + delimiter.length());
}
std::cout << b;
}
【问题讨论】:
-
如果两个数字不是写成字符串,你会如何相乘?
-
标准库中有用于从字符串转换的函数。谷歌一下。
-
@bipll:是的,它们被写在一个字符串中......将尝试转换。谢谢大家!
标签: c++ string split delimiter