std::string to integer type

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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?
 
K

Karthik

Christopher said:
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.
 
C

Christopher Benson-Manica

Karthik said:
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".
 
R

Russell Hanneken

Christopher Benson-Manica said:
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-lite/misc-technical-issues.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::eek:strstream 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top