I'm (intending to) use the code in this answer to read something from a CSV. Essentially I get an iterator to strings between consecutive ,
characters; but instead of putting them in a vector of strings, I want to parse these strings into elements of (arbitrary) type T, which comes from template argument. So...
template <typename T>
void foo(const std::string& line)
{
// ....
std::vector<T> vec;
using namespace boost;
tokenizer<escaped_list_separator<char> > tk(
line, escaped_list_separator<char>('\\', ',', '\"'));
for (tokenizer<escaped_list_separator<char> >::iterator i(tk.begin());
i!=tk.end();++i)
{
/* magic goes here */
}
I could use an istringstream` (e.g. as suggested here):
std::istringstream iss(*i);
T t; iss >> t;
vec.push_back(t);
But that's overkill (and I might be constructing twice or even three times here). If C++ had an std::from_string()
like its std::to_string
, then I would just do
vec.emplace_back(std::from_string(*i));
but that doesn't exist. Perhaps boost::lexical_cast
? I'd really rather using something standard.
What should I do instead?