cast string to size_t

Discussion in 'C++' started by pkirk25, Nov 13, 2006.

  1. pkirk25

    pkirk25 Guest

    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.
    pkirk25, Nov 13, 2006
    #1
    1. Advertising

  2. pkirk25

    Guest

    #include <sstream>

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

    You can check whether conversion failed by calling iss.fail()
    , Nov 13, 2006
    #2
    1. Advertising

  3. pkirk25

    Guest

    You should replace parameter of std::istringstream iss("a") with a
    value you want to convert.
    , Nov 13, 2006
    #3
  4. pkirk25

    benben Guest

    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
    benben, Nov 13, 2006
    #4
  5. pkirk25

    pkirk25 Guest

    Thanks.

    Is sstream preferred to atoi?
    pkirk25, Nov 13, 2006
    #5
  6. pkirk25

    Pete Becker Guest

    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.
    Pete Becker, Nov 13, 2006
    #6
  7. pkirk25

    Pete Becker Guest

    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.
    Pete Becker, Nov 13, 2006
    #7
  8. pkirk25

    Nate Barney Guest

    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
    Nate Barney, Nov 13, 2006
    #8
  9. pkirk25

    Nate Barney Guest

    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
    Nate Barney, Nov 13, 2006
    #9
  10. * 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?
    Alf P. Steinbach, Nov 13, 2006
    #10
  11. pkirk25

    Pete Becker Guest

    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.
    Pete Becker, Nov 13, 2006
    #11
  12. pkirk25

    pkirk25 Guest

    Thanks for the advice.
    pkirk25, Nov 13, 2006
    #12
  13. pkirk25

    Jack Klein Guest

    On Mon, 13 Nov 2006 22:42:27 +1100, benben
    <benhonghatgmaildotcom@nospam> wrote in comp.lang.c++:

    > 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()));


    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/~ajo/docs/FAQ-acllc.html
    Jack Klein, Nov 14, 2006
    #13
  14. pkirk25

    Pete Becker Guest

    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)
    Pete Becker, Nov 14, 2006
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,048
    Dan Pop
    Feb 10, 2004
  2. EvilRix
    Replies:
    8
    Views:
    605
    Martin Dickopp
    Feb 14, 2004
  3. Alex Vinokur
    Replies:
    9
    Views:
    763
    James Kanze
    Oct 13, 2008
  4. Pavel
    Replies:
    7
    Views:
    503
    Pavel
    Sep 19, 2010
  5. Alex Vinokur
    Replies:
    1
    Views:
    561
Loading...

Share This Page