std::string and strtod()

L

Leslaw Bieniasz

Cracow, 20.09.2004

Hi,

I want to replace:

char *text;
double val = strtod(text,NULL);

by an equivalent using std::string in the place of char *.

The construct:

std::string text;
double val = strtod(text.c_str(),NULL);

works, but isn't there any more elegant possibility,
specifically suited for std::string ?

Sincerely,

L.B.

*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
R

Ron Natalie

Leslaw Bieniasz said:
char *text;
double val = strtod(text,NULL);

by an equivalent using std::string in the place of char *.
double val;
std::istringstream(text) >> val;
 
M

Matt Hurd

Leslaw Bieniasz said:
Cracow, 20.09.2004

Hi,

I want to replace:

char *text;
double val = strtod(text,NULL);

by an equivalent using std::string in the place of char *.

The construct:

std::string text;
double val = strtod(text.c_str(),NULL);

works, but isn't there any more elegant possibility,
specifically suited for std::string ?

Sincerely,

L.B.

*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Electrochemical Oxidation of Gaseous Fuels, |
| ul. Zagrody 13, 30-318 Cracow, Poland. |
| tel./fax: +48 (12) 266-03-41 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*


A string stream is the normal approach which is encapsulated nicely
for you in boost::lexical_cast.

Reference: http://www.boost.org/libs/conversion/lexical_cast.htm

Example code FYI:

#include <iostream>
#include <boost/lexical_cast.hpp>

int main()
{
std::string s_value("3.141592654");

double d_value = boost::lexical_cast<double>(s_value);

std::cout << d_value << "\n";
}

It works the other way too, i.e. boost::lexical_cast<std::string>(
3.14 ) will give you a string.

HTH,

Matt Hurd
www.hurd.com.au
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top