Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::string and strtod()

Reply
Thread Tools

std::string and strtod()

 
 
Leslaw Bieniasz
Guest
Posts: n/a
 
      09-20-2004

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: |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      09-20-2004
Leslaw Bieniasz <> 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
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      09-20-2004

"Leslaw Bieniasz" <> wrote in message news-=
> char *text;
> double val = strtod(text,NULL);
>
> by an equivalent using std::string in the place of char *.
>

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

 
Reply With Quote
 
Matt Hurd
Guest
Posts: n/a
 
      09-21-2004
Leslaw Bieniasz <> wrote in message news:<>...
> 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: |
> *-------------------------------------------------------------------*
> | 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57