What is the fastest way to seperate these into 3 ints? I feel like this should be possible in one line but the closest I can find is this:
#include <ostream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
std::vector<int> extract_ints(std::string const& input_str)
{
std::vector<int> ints;
std::istringstream input(input_str);
std::string number;
while (std::getline(input, number, ':'))
{
std::istringstream iss(number);
int i;
iss >> i;
ints.push_back(i);
}
return ints;
}
int main()
{
typedef std::vector<int> ints_t;
ints_t const& ints = extract_ints("3254:23-45");
for (ints_t::const_iterator i = ints.begin(), i_end = ints.end(); i != i_end; ++i)
std::cout << *i << std::endl;
return 0;
}
And the link to that in action is here: https://godbolt.org/z/8sP9vvqfv but first of all it will only seperate between ':'s so far, not sure how to expand to ':'s and '-'s also. But secondly, this just seems overly complicated... Is this really the best method to do this?
Thanks