473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
Jul 22 '05 #1
5 4409
Christopher Benson-Manica <at***@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
Jul 22 '05 #2
Tim Love <tp*@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.
Jul 22 '05 #3
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.
Jul 22 '05 #4
Karthik <re*******************@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.
Jul 22 '05 #5

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c7**********@chessie.cirr.com...
Tim Love <tp*@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
rg********@pobox.com
Remove the 'g' from my address to send me mail.

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cheng Mo | last post by:
Below code cannot be compiled. try { throw std::string; } catch(...) { } While below is correct.
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
2
by: Susan Baker | last post by:
Hi, I have declared a class MyClass in a header file MyClass.h I have then gone onto define the class in MyClass.cpp. This is (roughly) what the definition (.cpp) file looks like: #include...
8
by: brightoceanlight | last post by:
I have an integer. I need to return it as a string. What is the best way to do this?
12
by: jl_post | last post by:
Dear C++ community, I have a question regarding the size of C++ std::strings. Basically, I compiled the following code under two different compilers: std::string someString = "Hello, world!";...
6
by: Nemok | last post by:
Hi, I am new to STD so I have some questions about std::string because I want use it in one of my projects instead of CString. 1. Is memory set dinamicaly (like CString), can I define for...
84
by: Peter Olcott | last post by:
Is there anyway of doing this besides making my own string from scratch? union AnyType { std::string String; double Number; };
3
by: silverburgh.meryl | last post by:
Hi, If I have a std string object , how to convert a STD string object to an integer? Thank you.
14
by: Mosfet | last post by:
Hi, what is the most efficient way of doing a case insensitive comparison ? I am trying to write a universal String class and I am stuck with the case insensitive part : TCHAR is a char in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.