C
Christopher Benson-Manica
What is the preferred C++ alternative to C's atoi(), if there is one?
Christopher Benson-Manica said:What is the preferred C++ alternative to C's atoi(), if there is one?
Jonathan Turkanis said:The standard alternative is to construct a stringstream and read its
data into an int using >>.
Christopher said:The standard alternative is to construct a stringstream and read its
data into an int using >>.
You would recommend such an approach in the following situation?
#include <cstdlib>
int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count=atoi( argv[1] ); // <- worth setting up a stringstream?
}
return EXIT_SUCCESS;
}
Christopher Benson-Manica said:Jonathan Turkanis said:The standard alternative is to construct a stringstream and read its
data into an int using >>.
You would recommend such an approach in the following situation?
#include <cstdlib>
int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count=atoi( argv[1] ); // <- worth setting up a stringstream?
}
return EXIT_SUCCESS;
}
I won't even use atoi in C. There are input conditions that cause it toChristopher Benson-Manica said:What is the preferred C++ alternative to C's atoi(), if there is one?
Christopher Benson-Manica said:Jonathan Turkanis <[email protected]> spoke thus:
#include <cstdlib>
int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count=atoi( argv[1] ); // <- worth setting up a stringstream?
}
return EXIT_SUCCESS;
}
Norbert Riedlin said:Christopher Benson-Manica said:Jonathan Turkanis <[email protected]> spoke thus:
#include <cstdlib>
int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count=atoi( argv[1] ); // <- worth setting up a stringstream?
}
return EXIT_SUCCESS;
}
What is the difference?
#include <boost/lexical_cast.hpp> // #include <cstdlib>
int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count = boost::lexical_cast<int>(argv[1]); // count=atoi( argv[1] );
}
// return EXIT_SUCCESS;
}
Why should one _not_ use lexical_cast in this/any case? Ok, to
have the same result here you had to surround the cast by a try
block, catching the appropriate boost::bad_lexical_cast but...
In my opinion, lexical_cast is to be preferred in _any_ realistical
case. In not realistical cases, noone cares.
Not at all. If you look closer, I was relying to Christopher's post. IJonathan Turkanis said:Do we disagree on anything? I mentioned the 'standard C++ alternative'
as well as boost::lexical cast; I did not express any preference.
Jonathan
Norbert Riedlin said:Christopher Benson-Manica said:Jonathan Turkanis <[email protected]> spoke thus:
#include <cstdlib>
int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count=atoi( argv[1] ); // <- worth setting up a stringstream?
}
return EXIT_SUCCESS;
}
What is the difference?
#include <boost/lexical_cast.hpp> // #include <cstdlib>
int main( int argc, char *argv[] )
{
int count=0;
if( argc > 1 ) {
count = boost::lexical_cast<int>(argv[1]); // count=atoi( argv[1] );
}
// return EXIT_SUCCESS;
}
Why should one _not_ use lexical_cast in this/any case? Ok, to
have the same result here you had to surround the cast by a try
block, catching the appropriate boost::bad_lexical_cast but...
In my opinion, lexical_cast is to be preferred in _any_ realistical
case. In not realistical cases, noone cares.
Bye
Norbert
Christopher Benson-Manica said:What is the preferred C++ alternative to C's atoi(), if there is one?
Christopher said:What is the preferred C++ alternative to C's atoi(), if there is one?
Nick Hounsome said:- std::strtol is also standard C and standard C++ and is superior in every
respect ( IMHO atoi should be deprecated ).
You are asuming the ASCII character set? In EBCDIC '9' > 'a'.Jeff Schwab said:template< typename N > // unsigned numeric type
N ato( char const* p, unsigned base =10 )
{
N result = N( );
while( *p )
{
result = result * base + ( *p <= '9' ? *p - '0' : *p - 'a' + 10 );
++p;
}
return result;
}
Norbert said:You are asuming the ASCII character set? In EBCDIC '9' > 'a'.
Bye
Norbert
Ok, so you are asuming correct and all lowercase input?Jeff Schwab said:No assumption, just a mistake. That should have said:
'0' <= *p && *p <= '9'
Norbert said:Ok, so you are asuming correct and all lowercase input?
Bye Norbert
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.