Thread: I would like some clarification regarding this iterator code.

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    33

    I would like some clarification regarding this iterator code.

    Can anyone explain why the author of this tutorial initially uses rbegin() but subsequently uses begin()?

    I thought the objective here was to be able to count backwards through the use of an iterator.

    Code:
    #include <iostream>
    #include <vector>
    
    int main ()
    {
      std::vector<int> myvector (5);  // 5 default-constructed ints
    
      int i=0;
    
      std::vector<int>::reverse_iterator rit = myvector.rbegin();
      for (; rit!= myvector.rend(); ++rit)
        *rit = ++i;
    
      std::cout << "myvector contains:";
      for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        std::cout << ' ' << *it;
      std::cout << '\n';
    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The first loop starts from the end of the vector (vector.at(4) and assigns values to the array. So vector.at(4) contains 1, at.(2) contains 3, etc. The second loop starts at the beginning of the vector printing each element.

    Jim

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    33
    Can anyone explain this piece of code?

    *rit = ++i;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by howardbc14
    Can anyone explain this piece of code?

    *rit = ++i;
    Break it down: you know ++i performs pre-increment, so starting from i = 0, it means that you get i = 1, then 2, then 3, etc. So, the statement assigns 1 to *rit on the first iteration, then 2 on the second iteration, etc. Of course, the object referred to by rit is different each time.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need clarification of the following code...
    By EAX101010 in forum C Programming
    Replies: 2
    Last Post: 09-22-2014, 06:43 AM
  2. Clarification....
    By CommonTater in forum C Programming
    Replies: 4
    Last Post: 09-23-2010, 05:39 PM
  3. Connecting input iterator to output iterator
    By QuestionC in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2007, 02:18 AM
  4. Help! Need some clarification.
    By jaro in forum C Programming
    Replies: 2
    Last Post: 04-08-2006, 11:44 AM
  5. iterator vs old-style c-code
    By curos in forum C++ Programming
    Replies: 4
    Last Post: 07-04-2005, 03:23 PM