cast string to size_t

P

pkirk25

I have a string with format "0|first_name|last_name|..." which i can
split to a vector<string> by the "|" token.

At the moment to get the row number I'm using size_t row_number =
atoi(buf_string[0].s_scr()) which throws warnings.

What is the correct way to convert a string "2" to size_t 2?

Thanks in advance.
 
O

ondra.holub

#include <sstream>

std::istringstream iss("a");
size_t size;
iss >> size;

You can check whether conversion failed by calling iss.fail()
 
O

ondra.holub

You should replace parameter of std::istringstream iss("a") with a
value you want to convert.
 
B

benben

pkirk25 said:
I have a string with format "0|first_name|last_name|..." which i can
split to a vector<string> by the "|" token.

At the moment to get the row number I'm using size_t row_number =
atoi(buf_string[0].s_scr()) which throws warnings.

I am guessing what you mean by s_scr() is in fact c_str(). If my guess
is correct then the following should get you a number

size_t row_number = size_t(atoi(buf_string[0].c_str()));

Notice the int-to-size_t conversion (not a cast.)
What is the correct way to convert a string "2" to size_t 2?

Thanks in advance.

Ben
 
N

Nate Barney

pkirk25 said:
Is sstream preferred to atoi?

std::stringstream is much more general than atoi. If you use atoi for
your problem, you'll be limited to the range of values that fit into an
int, whether or not you store the value in a size_t.

Also, atoi gives you no indication whether the string you passed to it
was an invalid string, or whether the string was "0". It returns 0 in
both cases.

Another alternative to atoi, if you don't want to use
std::istringstream, is the strtol family, but I don't believe those
functions are actually in the C++ standard. They're in the C99 standard
and a few others, so your compiler may support them.

Nate
 
N

Nate Barney

Nate said:
Another alternative to atoi, if you don't want to use
std::istringstream, is the strtol family, but I don't believe those
functions are actually in the C++ standard. They're in the C99 standard
and a few others, so your compiler may support them.

I neglected to mention that boost::lexical_cast is another good choice
for converting strings to numbers, and it's what I use almost
exclusively for this purpose.

Nate
 
A

Alf P. Steinbach

* Nate Barney:
Another alternative to atoi, if you don't want to use
std::istringstream, is the strtol family, but I don't believe those
functions are actually in the C++ standard.

They are.
 
P

Pete Becker

Nate said:
Another alternative to atoi, if you don't want to use
std::istringstream, is the strtol family, but I don't believe those
functions are actually in the C++ standard. They're in the C99 standard
and a few others, so your compiler may support them.

They're in C++ 2003, and as far as I know without checking, they were in
C++ 1998. The only ones that aren't there are the long long versions,
which were added to C with C99, and will be added to C++ in C++0x.

--

-- Pete
Roundhouse Consulting, Ltd. -- www.versatilecoding.com
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
 
J

Jack Klein

pkirk25 said:
I have a string with format "0|first_name|last_name|..." which i can
split to a vector<string> by the "|" token.

At the moment to get the row number I'm using size_t row_number =
atoi(buf_string[0].s_scr()) which throws warnings.

I am guessing what you mean by s_scr() is in fact c_str(). If my guess
is correct then the following should get you a number

size_t row_number = size_t(atoi(buf_string[0].c_str()));

Except, of course, that atoi(), atol(), and atof() should NEVER be
recommended or used, since they generate undefined behavior if the
result of the conversion is out of range for the return type.

That's why the strto...() functions were added to the C library 17
years ago.
 
P

Pete Becker

Jack said:
Except, of course, that atoi(), atol(), and atof() should NEVER be
recommended or used, since they generate undefined behavior if the
result of the conversion is out of range for the return type.

If you know the value is in range, they work just fine.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top