Topic: conversion of string to numeric


Author: brownsta@concentric.net (Stan Brown)
Date: 1998/12/12
Raw View
Tony@dontEmail.com (Tony) wrote:
>
>Is there a mechanism within Standard C++ (incl. the standard C++ libraries)
>for conversion of a 'string' to an 'int', 'float', or some other numeric?
>Something along the lines of:
>
>int myInt = myString.parseInt();

I've seen several suggestions for stringstreams.

Another possibility is good old strtol() and strtoul(), which have been
incorporated into the C++ library from the C library.

--
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA
                      http://www.concentric.net/%7eBrownsta/
"I'm not even supposed to BE here!"  -- the mantra from /Clerks/
My reply address is correct as is. The courtesy of providing a correct
reply address is more important to me than time spent deleting spam.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: AllanW@my-dejanews.com
Date: 1998/12/10
Raw View
In article <74lclk$4jv$1@news.ncal.verio.com>,
  "Tony" <Tony@dontEmail.com> wrote:
>
> Is there a mechanism within Standard C++ (incl. the standard C++ libraries)
> for conversion of a 'string' to an 'int', 'float', or some other numeric?

Look up atof() and atoi() in the C++ library.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Edward Diener <eddielee@abraxis.com>
Date: 1998/12/09
Raw View
#include <string>
#include <sstream>

std::string myString;
float f;
int i;

std::istringstream iss(myString);

iss >> f;

or

iss >> i;

Tony wrote:

> Is there a mechanism within Standard C++ (incl. the standard C++ libraries)
> for conversion of a 'string' to an 'int', 'float', or some other numeric?
> Something along the lines of:
>
> int myInt = myString.parseInt();
>
> or some other similarly OO style method?



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jcoffin@taeus.com (Jerry Coffin)
Date: 1998/12/10
Raw View
In article <74lclk$4jv$1@news.ncal.verio.com>, Tony@dontEmail.com
says...
>
> Is there a mechanism within Standard C++ (incl. the standard C++ libraries)
> for conversion of a 'string' to an 'int', 'float', or some other numeric?
> Something along the lines of:
>
> int myInt = myString.parseInt();
>
> or some other similarly OO style method?

Use a stringstream:

std::istringstream stream(myString);

stream >> myInt;

stream >> myFloat;

Basically, you can do any sort of extraction from the string that you
can from any other input stream.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Alex Martelli" <martelli@cadlab.it>
Date: 1998/12/10
Raw View
Tony wrote in message <74lclk$4jv$1@news.ncal.verio.com>...
>
>Is there a mechanism within Standard C++ (incl. the standard C++ libraries)
>for conversion of a 'string' to an 'int', 'float', or some other numeric?

Such mechanisms are provided through 'istringstream' (#include <sstream>).

>Something along the lines of:
>
>int myInt = myString.parseInt();
>
>or some other similarly OO style method?


I don't think you can do it contextually to a declaration, since streams
don't have method returning values (int, etc) as their results, but, rather,
operator>>, which takes the value to set (int, etc) by reference.  I.e.:

    int myInt;
    istringstream(myString) >> myInt;

It is, of course, easy to provide an inline wrapper template if you're
really keen to get a value-returning function:

    template<typename to>
    inline to parse(const string& str) {
        to t;
        istringstream(str)>>t;
        return t;
    }

    int myInt = parse<int>(myString);

Named-return-value optimization should hopefully make this
as efficient as one would wish.

Using a wrapper template also makes it easy and convenient
to throw an exception on bad-conversion, which streams don't
do by default in the Standard C++ library, but one would likely
wish to do in this case (the "naked", unwrapped construct above
does no validity checking at all on the string->int parsing).


Alex




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Tony" <Tony@dontEmail.com>
Date: 1998/12/09
Raw View
Is there a mechanism within Standard C++ (incl. the standard C++ libraries)
for conversion of a 'string' to an 'int', 'float', or some other numeric?
Something along the lines of:

int myInt = myString.parseInt();

or some other similarly OO style method?

Thanks,
Tony



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]