By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,415 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

cast string to size_t

P: n/a
I have a string with format "0|first_name|last_name|..." which i can
split to a vector<stringby 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.

Nov 13 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
#include <sstream>

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

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

Nov 13 '06 #2

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

Nov 13 '06 #3

P: n/a
pkirk25 wrote:
I have a string with format "0|first_name|last_name|..." which i can
split to a vector<stringby 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
Nov 13 '06 #4

P: n/a
Thanks.

Is sstream preferred to atoi?

Nov 13 '06 #5

P: n/a
benben wrote:
>
size_t row_number = size_t(atoi(buf_string[0].c_str()));

Notice the int-to-size_t conversion (not a cast.)
Huh? It's a cast.

--

-- 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.
Nov 13 '06 #6

P: n/a
pkirk25 wrote:
Thanks.

Is sstream preferred to atoi?
By some people, not by others.

--

-- 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.
Nov 13 '06 #7

P: n/a
pkirk25 wrote:
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
Nov 13 '06 #8

P: n/a
Nate Barney wrote:
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
Nov 13 '06 #9

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 13 '06 #10

P: n/a
Nate Barney wrote:
>
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.
Nov 13 '06 #11

P: n/a
Thanks for the advice.

Nov 13 '06 #12

P: n/a
On Mon, 13 Nov 2006 22:42:27 +1100, benben
<benhonghatgmaildotcom@nospamwrote in comp.lang.c++:
pkirk25 wrote:
I have a string with format "0|first_name|last_name|..." which i can
split to a vector<stringby 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.
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.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '06 #13

P: n/a
Jack Klein wrote:
>
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)
Nov 14 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.