471,077 Members | 763 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,077 software developers and data experts.

std::string and strtod()


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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
Jul 22 '05 #1
3 5307
Leslaw Bieniasz <nb******@cyf-kr.edu.pl> writes:

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 *.

See
http://www.parashift.com/c++-faq-lit....html#faq-38.2
Jul 22 '05 #2

"Leslaw Bieniasz" <nb******@cyf-kr.edu.pl> wrote in message news:Pi*******************************@kinga.cyf-=
char *text;
double val = strtod(text,NULL);

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

double val;
std::istringstream(text) >> val;

Jul 22 '05 #3
Leslaw Bieniasz <nb******@cyf-kr.edu.pl> wrote in message news:<Pi*******************************@kinga.cyf-kr.edu.pl>...
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: nb******@cyf-kr.edu.pl |
*-------------------------------------------------------------------*
| 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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Angus Leeming | last post: by
11 posts views Thread by Christopher Benson-Manica | last post: by
22 posts views Thread by Jason Heyes | last post: by
19 posts views Thread by Erik Wikström | last post: by
8 posts views Thread by Patrick Kowalzick | last post: by
6 posts views Thread by Nemok | last post: by
84 posts views Thread by Peter Olcott | last post: by
11 posts views Thread by Jacek Dziedzic | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.