Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cast string to size_t

Reply
Thread Tools

cast string to size_t

 
 
pkirk25
Guest
Posts: n/a
 
      11-13-2006
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.

 
Reply With Quote
 
 
 
 
ondra.holub@post.cz
Guest
Posts: n/a
 
      11-13-2006
#include <sstream>

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

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

 
Reply With Quote
 
 
 
 
ondra.holub@post.cz
Guest
Posts: n/a
 
      11-13-2006
You should replace parameter of std::istringstream iss("a") with a
value you want to convert.

 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      11-13-2006
pkirk25 wrote:
> 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
 
Reply With Quote
 
pkirk25
Guest
Posts: n/a
 
      11-13-2006
Thanks.

Is sstream preferred to atoi?

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-13-2006
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.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      11-13-2006
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.
 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      11-13-2006
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
 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      11-13-2006
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-13-2006
* 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?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reinterpret_cast<std::size_t>(p) and reinterpret_cast<std::size_t&>() Alex Vinokur C++ 1 02-06-2011 07:48 AM
Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>* Alex Vinokur C++ 9 10-13-2008 05:05 PM
error C2440: 'return' : cannot convert from 'const char *' to 'const unsigned short *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Abhijit Bhadra C++ 2 12-01-2004 04:43 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM



Advertisments