Type for string index

P

Phil Endecott

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

Victor Bazarov

Phil said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top