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. Advertising

  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. 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. karthikeyavenkat
    Replies:
    2
    Views:
    671
    Bryce
    Mar 17, 2005
  2. Shawn W_
    Replies:
    5
    Views:
    320
    Aldric Giacomoni
    Sep 16, 2009
  3. ngoc
    Replies:
    5
    Views:
    199
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

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

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

Share This Page