atoi and std::string

Started by
8 comments, last by CProgrammer 20 years, 6 months ago
How can i use atoi on std::string or is there another way to receive the number represented by a std::string object?
Advertisement
to use atoi:
std::string snumber = "42";
int i = atoi(snumber.c_str());

I almost always use atoi, but there are better ways I''m told.

I think stlsoft has converters...

you could use a stringstream and inserters/extracters:
stringstring sstrm(snumber);
sstrm >> i;

There are more. I''m sure somebody will enlighten us as to the "best" way
What is Barrel Drop?
Who are HTS Games?
Yes, use C++ string streams (std::stringstream, std::istringstream, std::ostringstream), defined in sstream.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Thanks a bunch,
this version will do fine: int i = atoi(snumber.c_str());

-CProgrammer
atoi isnt standard c++

Use stringstreams, its just as easy

template<typename RT, typename T, typename Traits,typename alloc>RT better_atoi(const basic_string<T,Traits, alloc>& your_string){RT i;std::stringstream tempstream = your_string;tempstream >> i;return i;}


Not tested that.. correct me if its wrong
quote:Original post by Anonymous Poster
atoi isnt standard c++

Of course it is. It''s in C89, so it''s in C++. You''re thinking of itoa.

How appropriate. You fight like a cow.
quote:Original post by Anonymous Poster
atoi isnt standard c++

Use stringstreams, its just as easy

template<typename RT, typename T, typename Traits,typename alloc>RT better_atoi(const basic_string<T,Traits, alloc>& your_string){RT i;std::stringstream tempstream = your_string;tempstream >> i;return i;}


Not tested that.. correct me if its wrong

It's not so much that it's wrong, it's just it could be more generic. See boost::lexical_cast. Also note that initialisation of the basic_stringstream (hint) object, using its constructor, would be more favourable.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 11, 2003 4:57:51 PM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
template<typename RT, typename T, typename Traits, typename Alloc>RT even_better_atoi(const basic_string<T,Traits,Alloc>& my_string){RT temp_1basic_istringstream<T,Traits,Alloc> temp_string = my_string;tempstring >> temp_1;return temp_1;}


La?

"Also note that initialisation of the basic_stringstream (hint) object, using its constructor, would be more favourable. "

Thats what the code does.. temp_string(my_string) is the same as temp_string = my_string ... Both call the constructor with the my_string parameter
Yes, correct. It's "syntactic sugar". Fleh, switching between languages.

You're sort of nearly there. You need to choose to instantiate the string stream template with the right character type based on both the target and source types. Also it can be improved further (exceptions, limits/precision, etc.). As I've already said, boost::lexical_cast.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 12, 2003 5:55:34 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
"atoi isnt standard c++"

Small point, but all the old C stuff that''s bundled in C++ *is* standard C++, meaning that a standards conforming compiler *must* support it. printf, strcpy, memcpy, etc. etc. etc. are all C++, but they are also C.

--
Dave Mikesell
d.mikesell@computer.org
http://davemikesell.com

This topic is closed to new replies.

Advertisement