1. random access iterator

Hello everyone,

For random access iterator, operator[] is supported. Mentioned in Bjarne's book, Chapter 19 (Iterators and Allocators).

I have not used operator[] on random access iterator before and I have not found a good and simple sample either. :-)

Could anyone show its usage with some pseudo code please?

George

2. For random access iterator, operator[] is supported. Mentioned in Bjarne's book, Chapter 19 (Iterators and Allocators).
What exactly did Stroustrup write? Are you sure he did not say "for random access, operator[] is provided"?

3. Hi laserlight,

If you have the book at hand, you can find section 19.2.1, there is a table about iterator operations and categories. For random access iterator, -> and [] are supported.

Originally Posted by laserlight
What exactly did Stroustrup write? Are you sure he did not say "for random access, operator[] is provided"?

regards,
George

4. "supported" not provided. You can overload them to allow random access. They are not made that way.

5. There's no difference. Random access iterators have to implement [] with the logical semantics. The semantics are equivalent to those of a pointer.

As such, it[3] and *(it+3) are functionally equivalent.

6. Thanks CornedBee,

Originally Posted by CornedBee
There's no difference. Random access iterators have to implement [] with the logical semantics. The semantics are equivalent to those of a pointer.

As such, it[3] and *(it+3) are functionally equivalent.
Hi Raigne,

I have no idea what you mentioned. Could you provide some pseudo code please?

regards,
George

7. Pseudo code? For what? it[3] and *(it+3) are equivalent. You should know what it+3 does. You should know what dereferencing the result does. Thus, you should know what the whole right expression does, and thus you know what the indexing operator does.

8. Hi CornedBee,

I do not mean pseudo code for your reply. I mean pseudo code for Raigne's reply in post #4. I do not know what he means. :-)

Originally Posted by CornedBee
Pseudo code? For what? it[3] and *(it+3) are equivalent. You should know what it+3 does. You should know what dereferencing the result does. Thus, you should know what the whole right expression does, and thus you know what the indexing operator does.

regards,
George

9. Raigne meant you have to DIY (he/she probably meant override not overload too. correct me if i'm wrong). i.e. we can't just call [] on an object and expect it to do what we want. for example you're creating a string class. you probably want [] to access the individual characters in your string. the compiler doesn't know how to do that, because there can be many different implementations of a string.

i've a question here too. if i have a class A, and I override [], and then I instantiate an array of As, how do I access the individual A instances, and how do I access the [] operator I overrided?

10. He meant overload, which is what you do with operators. Overriding is what you do with virtual functions you inherited.

Regarding your question, the [] applied to the array will get you a specific instance. The overloaded [] only comes into play when you apply it to a specific instance.

11. that makes perfect sense! thanks cornedbee!
though, what if i want to access the overloaded [] from a specific instance of an array?
is this what i do?

myclasses[a][b] = something; // myclasses is an array of objects

12. Yes. The first [] applies to the array, the second to the instance.

13. Thanks CornedBee,

Do you mean in the sample code,

Code:
`myclasses[a][b] = something;`
operator[] is invoked by the a-th instance of the array, and b and something are two input parameters for operator[] for the a-th element of the instance array?

Originally Posted by CornedBee
Yes. The first [] applies to the array, the second to the instance.

Hi jian2587,

Originally Posted by jian2587
for example you're creating a string class. you probably want [] to access the individual characters in your string. the compiler doesn't know how to do that, because there can be many different implementations of a string.
I do not know what you mean above. Could you show some pseude code please?

BTW: operator[] on string works good. Here is my test code, I do not know why you say it can not work.

Code:
```#include <string>

using namespace std;

int main()
{
string str ("abcdefg");

char ch = str[3]; // ch is 'd'

return 0;
}```

regards,
George

14. oh george2, i actually meant your OWN implementation of a string class. sorry for the confusion.

15. Hi CornedBee,

Any comments to my confusions in the following part in post #13?

--------------------
Do you mean in the sample code,

Code:
`myclasses[a][b] = something;`
operator[] is invoked by the a-th instance of the array, and b and something are two input parameters for operator[] for the a-th element of the instance array?
--------------------

Originally Posted by jian2587
oh george2, i actually meant your OWN implementation of a string class. sorry for the confusion.

regards,
George