1

如果我有一个ostringstream包含各种数字的-

(那是:space - space顺便说一句)

我可以单独提取每个数字吗?

4

2 回答 2

2

使用此处的吐痰功能之一: Split a string in C++? 将它们作为字符串存储在 std::vector 中,然后使用 std::stoi (或等效的)将字符串解析为 integer ,用 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";
    }
}
于 2015-01-22T14:50:20.733 回答
0

像这样的东西可能会起作用....

#include <boost/algorithm/string.hpp>
std::vector<int> ints;
boost::split(ints, " - ",std::stoi (stream.str()));
于 2015-01-22T14:04:36.557 回答