Thread: Iterators

  1. #1
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738

    Iterators

    I have read plenty of times about iterators or constant iterators. What does their name mean and what do they do?
    I understand they have something to do with memory manipulation but no matter how much code i read, i just can't get their meaning!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sipher
    I have read plenty of times about iterators or constant iterators. What does their name mean and what do they do?
    The concept of an iterator generalises the concept of a pointer, and in fact pointers are random access iterators. When you see const_iterator, it means "iterator to const" rather than "iterator that is const". Among other things, they allow certain generic algorithms to be separated from containers: the user provides iterators to the algorithm to denote a range within the container (or otherwise), and the algorithm can require certain categories of iterators to work (i.e., some iterators are more powerful than others).
    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 UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    well, an iterator is an object that allows a programmer to traverse through all
    the elements of a collection (like std:: ), regardless of its specific implementation.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    So, you're saying that iterators are useful for developing DLLs and the like?

    Is an iterator just an shared interface or something more?

    (I don't know if i understood what you said laserlight, i have to brush up my english)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Not really. An iterator is an abstract way of representing something that can step through the elements of a container. A container is something like an array, list, map, vector, set, etc. An iterator is something that allows you to step through each element in that container. For example, let's say you have a list:
    Code:
    std::list<int> list_of_numbers;
    Then you add the numbers 1 - 5 to the list:
    Code:
    for (int i = 1; i <= 5; ++i)
        list_of_numbers.push_back(i);
    Later on you want to go through each number in the list and add them all up. To do that, you could use an iterator to step through them one at a time and add them to the sum:
    Code:
    int sum = 0;
    
    // Create a new iterator and point to the beginning of the list:
    std::list<int>::iterator current_element = list_of_numbers.begin();
    
    // Loop while the iterator doesn't point to the end of the list
    while (current_element != list_of_numbers.end())
    {
        // Use * to get the value at that point in the list. Add the value to the sum:
        sum += *current_element;
    
        // Increment the iterator to the next element in the list:
        ++current_element;
    }
    
    std::cout << "The sum is " << sum << ".\n";
    In this case it would output 15, since 1 + 2 + 3 + 4 + 5 = 15.

    A const_iterator just means you promise not to change the values in the container, so you could have (and should have) made current_element a std::list<int>::const_iterator in the previous example.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Oh, now i got it!! Thanks for the quick reply guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. algorithms over vector: offsets or iterators for speed?
    By pheres in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2009, 02:23 AM
  2. Subtracting offsets from iterators
    By Beamu in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2008, 03:12 AM
  3. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  4. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 03:19 AM
  5. Writing Iterators...
    By Geolingo in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM