Thread: random access iterator

  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
    28,413
    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"?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,895
    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,895
    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,895
    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,895
    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

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, 08: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, 08:06 AM