Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   std::string to integer type (http://www.velocityreviews.com/forums/t282806-std-string-to-integer-type.html)

Christopher Benson-Manica 05-03-2004 02:47 PM

std::string to integer type
 
Are the C functions atoi() and strtol() (preferred, I'm aware) the
only way to convert std::string's to integers, or are there other
alternatives I'm not aware of?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Tim Love 05-03-2004 02:51 PM

Re: std::string to integer type
 
Christopher Benson-Manica <ataru@nospam.cyberspace.org> writes:

>Are the C functions atoi() and strtol() (preferred, I'm aware) the
>only way to convert std::string's to integers, or are there other
>alternatives I'm not aware of?

See the FAQ - e.g.
http://www.parashift.com/c++-faq-lit...al-issues.html

Christopher Benson-Manica 05-03-2004 03:06 PM

Re: std::string to integer type
 
Tim Love <tpl@eng.cam.ac.uk> spoke thus:

> See the FAQ - e.g.
> http://www.parashift.com/c++-faq-lit...al-issues.html


Oops!!! I should have known better - sorry for not reading the holy
writ first :( And unfortunately, I can't use the C++ solution
either, since stringstreams are hit or miss on my (horribly broken)
system. Oh well.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Karthik 05-03-2004 03:25 PM

Re: std::string to integer type
 
Christopher Benson-Manica wrote:
> I can't use the C++ solution
> either, since stringstreams are hit or miss on my (horribly broken)
> system. Oh well.


Do you mean to say, this is not in accordance with the standard (
and hence, UB ? ) . I am sorry I didnt understand this.

--
Karthik.
Humans please 'removeme_' for my real email.

Christopher Benson-Manica 05-03-2004 03:59 PM

Re: std::string to integer type
 
Karthik <removeme_kaykaydreamz@yahoo.com> spoke thus:

> Do you mean to say, this is not in accordance with the standard (
> and hence, UB ? ) . I am sorry I didnt understand this.


No, it means that for a variety of reasons, stringstreams don't behave
on my system. None of those reasons involve UB - it can be summed up
succinctly by saying "My system sucks".

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Russell Hanneken 05-03-2004 04:03 PM

Re: std::string to integer type
 

"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c75n54$lb5$1@chessie.cirr.com...
> Tim Love <tpl@eng.cam.ac.uk> spoke thus:
>
> > See the FAQ - e.g.
> > http://www.parashift.com/c++-faq-lit...al-issues.html

>
> Oops!!! I should have known better - sorry for not reading the holy
> writ first :( And unfortunately, I can't use the C++ solution
> either, since stringstreams are hit or miss on my (horribly broken)
> system. Oh well.


I have two suggestions:

1. See if you can use lexical_cast from the Boost library:

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

It's an elegant solution, and it looks like the author made an attempt to
work around broken C++ implementations.

2. I've rewritten the code in the FAQ to use the deprecated strstream
classes instead of the stringstream classes. Maybe it will work for you:

#ifndef CONVERT_H
#define CONVERT_H

// File: convert.h

// Based on code from the C++ FAQ Lite:
// http://www.parashift.com/c++-faq-lit....html#faq-38.3
// Modified to use strstream classes instead of stringstream classes.

#include <istream>
#include <ostream>
#include <stdexcept>
#include <string>
#include <strstream>
#include <typeinfo>

class BadConversion : public std::runtime_error {
public:
BadConversion(const std::string& s)
: std::runtime_error(s)
{ }
};

template<typename T>
inline std::string stringify(const T& x)
{
std::ostrstream o;
if (!(o << x << std::ends))
throw BadConversion(std::string("stringify(")
+ typeid(x).name() + ")");
std::string s(o.str());
o.freeze(false);
return s;
}

template<typename T>
inline void convert(const std::string& s, T& x,
bool failIfLeftoverChars = true)
{
std::istrstream i(s.c_str());
char c;
if (!(i >> x) || (failIfLeftoverChars && i.get(c)))
throw BadConversion(s);
}

template<typename T>
inline T convertTo(const std::string& s,
bool failIfLeftoverChars = true)
{
T x;
convert(s, x, failIfLeftoverChars);
return x;
}

#endif

--
Russell Hanneken
rghanneken@pobox.com
Remove the 'g' from my address to send me mail.





All times are GMT. The time now is 12:39 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.