<分区>
如果我有一个 ostringstream
,其中包含由 -
分隔的各种数字
(那是:space
-
space
btw)
我可以单独提取每个数字吗?
<分区>
如果我有一个 ostringstream
,其中包含由 -
分隔的各种数字
(那是:space
-
space
btw)
我可以单独提取每个数字吗?
最佳答案
从这里使用一个 spiting 函数: Split a string in C++? 将它们存储为 std::vector 中的字符串, 然后使用 std::stoi (或等效的)将字符串解析为整数,用 try/catch 包围每个调用。
示例(拆分后):
for (int i = 0; i < arrayOfStrings.size(); i++)
{
try
{
int myInt = std::stoi(arrayOfStrings[i]);
}
catch (std::exception& e)
{
std::cout<<e.what()<<"\n";
}
}
关于c++仅从ostringstream中提取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28090351/