std::string and strtod()

Discussion in 'C++' started by Leslaw Bieniasz, Sep 20, 2004.

  1. 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 |
    *-------------------------------------------------------------------*
     
    Leslaw Bieniasz, Sep 20, 2004
    #1
    1. Advertising

  2. Leslaw Bieniasz

    Tim Love Guest

    Tim Love, Sep 20, 2004
    #2
    1. Advertising

  3. Leslaw Bieniasz

    Ron Natalie Guest

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

    double val;
    std::istringstream(text) >> val;
     
    Ron Natalie, Sep 20, 2004
    #3
  4. Leslaw Bieniasz

    Matt Hurd Guest

    Leslaw Bieniasz <> wrote in message news:<-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: |
    > *-------------------------------------------------------------------*
    > | 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
     
    Matt Hurd, Sep 21, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mathieu Malaterre

    strtod / setlocale

    Mathieu Malaterre, Dec 13, 2004, in forum: C++
    Replies:
    1
    Views:
    578
    Buster
    Dec 13, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,441
    Ivan Vecerina
    Mar 17, 2005
  3. Fei Liu
    Replies:
    9
    Views:
    472
  4. Jeffrey Walton
    Replies:
    10
    Views:
    978
    Mathias Gaunard
    Nov 26, 2006
  5. Juha Nieminen
    Replies:
    5
    Views:
    3,225
    Michael DOUBEZ
    Oct 24, 2007
Loading...

Share This Page