Iterators

This is a discussion on Iterators within the C++ Programming forums, part of the General Programming Boards category; I have read plenty of times about iterators or constant iterators. What does their name mean and what do they ...

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

    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
    21,310
    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).
    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 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
    1,616
    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,317
    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
    1,616
    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, 02: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, 02:19 AM
  5. Writing Iterators...
    By DeepBlackMagic in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM

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