String to int/double conversion and parsing

K

kaede

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
 
D

David Hilsee

kaede said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top