random access iterator

This is a discussion on random access iterator within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, For random access iterator, operator[] is supported. Mentioned in Bjarne's book, Chapter 19 (Iterators and Allocators). I have ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    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?


    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,113
    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"?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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.

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

    regards,
    George

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    "supported" not provided. You can overload them to allow random access. They are not made that way.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Your description is clear.

    Quote Originally Posted by CornedBee View Post
    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. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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. :-)

    Quote Originally Posted by CornedBee View Post
    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. #9
    Registered User jian2587's Avatar
    Join Date
    Feb 2008
    Location
    NY
    Posts
    11
    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. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User jian2587's Avatar
    Join Date
    Feb 2008
    Location
    NY
    Posts
    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. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Yes. The first [] applies to the array, the second to the instance.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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?

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

    Hi jian2587,


    Quote Originally Posted by jian2587 View Post
    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
    Last edited by George2; 03-02-2008 at 07:38 PM.

  14. #14
    Registered User jian2587's Avatar
    Join Date
    Feb 2008
    Location
    NY
    Posts
    11
    oh george2, i actually meant your OWN implementation of a string class. sorry for the confusion.

  15. #15
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    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?
    --------------------

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

    regards,
    George

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:42 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. random access function table design?
    By talz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2005, 03:30 PM
  4. linked list iterator class inheritience
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 10:03 AM
  5. Random Access Files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 09:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21