Thread: incrementing list iterators

  1. #1
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18

    incrementing list iterators

    I'm having a little problem here...

    If I have this:
    int N = 5;
    list <type> newlist;
    list <type>::iterator it = newlist.begin();
    it = it + N;

    on the bold line above, it doesn't let me do it, doesn't even compile.

    How can I increment a list iterator N units forward?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess you're looking for advance().
    like this
    Code:
    #include<iterator>
    #include<list>
    #include<iostream>
    
    using namespace std;
    
    int main() {
    
        list<int> l;
        l.insert(l.end(), 1);
        l.insert(l.end(), 2);
        l.insert(l.end(), 3);
        l.insert(l.end(), 4);
    
        list<int>::iterator itr = l.begin();
    
        advance(itr,2);
        cout << *itr << endl;
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    yeah it works now, thanks very much ^^

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Could someone explain why it= it + N doesn't work? It works with this

    Code:
    vector<int> the_vector;
      vector<int>::iterator the_iterator;
      for( int i=0; i < 10; i++ )
        the_vector.push_back(i);
      int total = 0;
      the_iterator = the_vector.begin();
      while( the_iterator != the_vector.end() ) {
        total += *the_iterator;
        the_iterator++;
      }
      cout << "Total=" << total << endl;

  5. #5
    Registered User
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    18
    Quote Originally Posted by jk1998 View Post
    Could someone explain why it= it + N doesn't work? It works with this

    Code:
    vector<int> the_vector;
      vector<int>::iterator the_iterator;
      for( int i=0; i < 10; i++ )
        the_vector.push_back(i);
      int total = 0;
      the_iterator = the_vector.begin();
      while( the_iterator != the_vector.end() ) {
        total += *the_iterator;
        the_iterator++;
      }
      cout << "Total=" << total << endl;
    you are working with vectores there not lists

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    List iterators are bidirectional iterators, not random access iterators. Consequently, you can do ++iter, iter++, --iter, and iter-- with list iterators, but not iter += n, iter + n, iter -= n, or iter - n. advance() in this case would just apply ++iter twice to simulate iter + 2.
    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

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Thanks.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that technically it + N could be allowed in that it could just do the same thing that advance does. It was purposefully left out because that operation can be slow. If you find yourself using advance with lists often, then you might want to reconsider whether a list is the appropriate container for your needs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM