1

If I have an ostringstream that contains various numbers separated by -

(That's: space - space btw)

Could I extract each number individually?

fakeaccount
  • 933
  • 4
  • 13
  • 23

2 Answers2

2

use one of spiting functions from here: Split a string in C++? storing them as strings inside std::vector, then use std::stoi (or equivalent) which parses string to integer , surrounding each call with try / catch.

example (after splitting):

for (int i = 0; i < arrayOfStrings.size(); i++)
{
    try
    {
        int myInt = std::stoi(arrayOfStrings[i]);
    }
    catch (std::exception& e)
    {
        std::cout<<e.what()<<"\n";
    }
}
Community
  • 1
  • 1
David Haim
  • 25,446
  • 3
  • 44
  • 78
0

Something like this may work....

#include <boost/algorithm/string.hpp>
std::vector<int> ints;
boost::split(ints, " - ",std::stoi (stream.str()));
Stefano
  • 3,981
  • 8
  • 36
  • 66