By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,812 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,812 IT Pros & Developers. It's quick & easy.

String to int/double conversion and parsing

P: n/a
Hi all,

Given the following data format: { "(1,2), "(2.5, 3.5)", .... }

I would like to define a vector<string> to stored this data:

v[0] = "(1,2)", v[1] = "(2.5, 3.5"), ....

and would like to extract the integer/double string value to real
int/double and use it in computation. What is the best way to do it?
Currently, I am using C routine atoi() and wrote a litter paring
function to extract the int/double value. I would like to know if
there is a better way to do in using STL and maybe its algorithm?

THanks,
Kaede
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"kaede" <ka************@hotmail.com> wrote in message
news:60**************************@posting.google.c om...
Hi all,

Given the following data format: { "(1,2), "(2.5, 3.5)", .... }

I would like to define a vector<string> to stored this data:

v[0] = "(1,2)", v[1] = "(2.5, 3.5"), ....

and would like to extract the integer/double string value to real
int/double and use it in computation. What is the best way to do it?
Currently, I am using C routine atoi() and wrote a litter paring
function to extract the int/double value. I would like to know if
there is a better way to do in using STL and maybe its algorithm?


Ditch atoi() and use something that has more reasonable behavior when passed
invalid input, like a stringstream. Other than that, the best solution
would probably involve iterating over the std::vector and parsing each
element (e.g. std::pair<double,double> parseDoubles(const std::string& s)).
You might be able to come up with something that uses <algorithm>, like
std::transform, but the code would probably look unnatural.

--
David Hilsee
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.