ThomasMcLeod
ThomasMcLeod

Reputation: 7769

Is string::iterator necessarily a random_access_iterator?

This page states that string::iterator and string::const_iterator are "compiler specific iterator types." Does this mean that that string::iterator made be in a category other than random_access_iterator?

Upvotes: 6

Views: 583

Answers (4)

mfontanini
mfontanini

Reputation: 21900

ISO C++03, 21.3-2 states:

(...)Additionally, because the iterators supported by basic_string are random access iterators(...)

So yes, those are necessarily random access iterators.

Upvotes: 12

Jonathan Wakely
Jonathan Wakely

Reputation: 171263

It doesn't mean it's not a random access iterator (as other answers have said, the standard requires it to be a random access iterator.)

What it means is the exact iterator types are unspecified, i.e. they could be char* or they could be std::string::__iterator or __gnu_cxx::__normal_iterator<char, std::string> or something else. Any of those implementation choices is allowed, as long as it meets the requirements of a random access iterator and can be referred to by the name string::iterator

Upvotes: 3

Qaz
Qaz

Reputation: 61910

This page says std::string::iterator is a RandomAccessIterator, and is a lot more trustworthy than cplusplus.com.

Upvotes: 3

pmr
pmr

Reputation: 59811

Looking at N3376 21.4/3

The iterators supported by basic_string are random-access iterators.

For such tasks its always a good idea to have a standard (or draft) at hand. It takes a while to get used to looking up things there, but it is the first hand resource.

Upvotes: 5

Related Questions