Type for string index

Discussion in 'C++' started by Phil Endecott, Jun 7, 2005.

  1. Dear C++ Experts,

    What type should I use for an index into a std::string? In the past I
    have used an unsigned int, e.g.

    unsigned int p = s.find("foo");
    string t = s.substr(p,10);
    etc. etc.

    but I now find that this doesn't work on 64-bit machines; I get warnings
    when I compare with npos saying that the comparison will always be false
    due to the different ranges of the types.

    My guess is that I should be using std::string::size_type. Is this right?

    --Phil.
     
    Phil Endecott, Jun 7, 2005
    #1
    1. Advertisements

  2. Phil Endecott wrote:
    > What type should I use for an index into a std::string? [...]
    >
    > My guess is that I should be using std::string::size_type. Is this right?


    Yes.
     
    Victor Bazarov, Jun 7, 2005
    #2
    1. Advertisements

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. karthikeyavenkat
    Replies:
    2
    Views:
    947
    Bryce
    Mar 17, 2005
  2. heyo
    Replies:
    3
    Views:
    1,340
    Dan Pop
    Apr 1, 2004
  3. Vikram
    Replies:
    7
    Views:
    2,940
    Andrew Thompson
    Jun 4, 2008
  4. Greg Hurrell
    Replies:
    1
    Views:
    318
    Greg Hurrell
    Feb 26, 2007
  5. Shawn W_
    Replies:
    5
    Views:
    535
    Aldric Giacomoni
    Sep 16, 2009
  6. ngoc
    Replies:
    5
    Views:
    373
    Tad McClellan
    May 11, 2006
  7. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    586
    Tomasz Chmielewski
    Mar 4, 2008
  8. Stefan Mueller
    Replies:
    7
    Views:
    338
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...