Thread: iterator

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002


    I have found the following code :

    #include <algorithm>
    #include <iostream>
    #include <list>
    #include <set>
    #include <string>
    using namespace std;
    int main()
    	typedef set<string> WordSet;
    	typedef WordSet::iterator WordIter;
    	typedef list<WordIter> Index;
    	WordSet words;
    	Index input_order;
    	// Input the words uniquely
    	for(string new_word; cin >> new_word, new_word!="Quit"; )
    		pair<WordIter, bool> const &trace = words.insert(new_word);
    		if (trace.second)
    	// Output unique words in order of input:
    	cout << "\n>>> Unique words in input order:\n";
    	for (Index::iterator p = input_order.begin(); p!=input_order.end(); ++p)
    		cout << *(*p) << '\n';
    	// Output unique words in default set<string> order:
    	cout << "\n>>> Unique words in sorted order:\n";
    	copy(words.begin(), words.end(), ostream_iterator<string>(cout, "\n"));
    	return 0;
    what is the iterator thing?
    Is it like a cin or just a variable that holds a value? .. or something else?


  2. #2
    Registered User
    Join Date
    Mar 2002
    think of it as an object that acts like a glorified generalization of a pointer and you will be pretty close.

  3. #3
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002


    is iterator part of the stl?
    i mean in <set> maybe?


  4. #4
    Registered User
    Join Date
    Mar 2002
    yes, I've never seen iterators (as such) used outside of STL (I have seen smart pointers which are similar to iterators outside of STL). Iterators are objects and need to be declared for each container they are associated with. While you can write your own iterator class, the containers in STL come with their own. Unlike pointers, iterators come in a variety of flavors such as forward iterators, reverse iterators, bidirectional iterators, etc. For straightforward application of the concept you can just declare an iterator using syntax similar to that in the code you provided and get by with it. With more sophisticated applications the full flexibility of iterators becomes more relevant. You can read about iterators in any text that talks about STL containers.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    When we iterate through a list for instance, we usually have to reference the node through which the data is stored.

    if(iter->data > 10)
    iter->data = 0;

    iter = iter->next;

    Iterators overload the traversing pointer to return a pointer to the data. Also, the iterator is overloaded with the post/pre increment/decrement operators to further abstract the implementation:

    if( *iter > 10 )
    *iter = 0;

    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-20-2008, 12:16 AM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 11:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 09:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM
Website Security Test