Iterators

Im just starting to learn about vectors and I cannot grasp the concept of iterators and const interators and what they do. How do they work? Ive read the section in the book over and over again and tried practice problems and I just dont understand
Last edited on
An iterator is an object that pretends it is a pointer.


For example, you can print all the elements in a c-string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

void print( const char* begin, const char* end )
{
  while (begin != end)
  {
    cout << *begin;
    ++begin;
  }
  cout << "\n";
}

int main()
{
  const char message[] = "Hello world!";
  print( message, message + 12 );
}

The 'begin' and 'end' objects are pointers to the characters in the string.

'begin' points to the first character
'end' points to one-past the last character


Now, pointers are all fine and dandy, and make things easy, but what about sequences like linked lists? Playing with pointers gets more complicated.

The idea of iterators is to make it simple again, by creating an object that pretends it is a pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <list>
using namespace std;

template <typename Iterator>
void print( Iterator begin, Iterator end )
{
  while (begin != end)
  {
    cout << *begin;
    ++begin;
    cout << "\n";  // Yeah, I moved this into the loop
  }
}

int main()
{
  list <const char*> names;
  names.push_back( "Malcolm" );
  names.push_back( "Zoe" );
  names.push_back( "Kaylee" );
  names.push_back( "Jayne" );
  // and so on...

  // Now we can iterate through the list with our handy pointer-like syntax
  print( names.begin(), names.end() );
}

Hope this helps.
Hmmm that helps but how does an interator know what vector to go through if theres multiple vectors? For example my book gives
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Int main()
{
   vector<string> inventory;
   inventory.push_back("sword");
   inventory.push_back("armour");
   inventory.push_back("shield");

   vector<string>::iterator myIterator;
   vector<string>::const_iterator iter;

   cout  << "Your items:\n";
   for (iter=inventory.begin(); iter != inventory.end(); ++iter)
     {
       cout  << *iter << endl;
     }
}


How does myIterator know to go through vector inventory if there are other vectors. Or is iter what goes through the vecotr and myIterator is just a holding block for new information
Last edited on
I see that you are not using myIterator.
iter knows which vector to iterate through (in this case inventory) as you have iter=inventory.begin(); iter != inventory.end();
Well I didnt give you the full code the book used. Later in the code they added 'battle axe' to the beginning of the vector so they did
 
*myIterator = "battle axe";
@code does it kbow what vector to go through because you declare it right before or after the vector? How does it know, thats what I want to know
The iterator is specific to the vector, it knows because what may be happening behind the scene of begin() and end().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>

struct MyArray  // note that structs are classes that are public by default
{
  struct Iterator
  {
    int *m_ptr;

    Iterator( int *ptr )
      : m_ptr( ptr )
    {
    }

    int operator*( void ) const
    {
      return *m_ptr;
    }

    bool operator!=( const Iterator& other ) const
    {
      return m_ptr != other.m_ptr;
    }

    Iterator& operator++( void )
    {
      ++m_ptr;
      return *this;
    }
  };

  int *m_data;
  const int m_size;


  MyArray( const int size )
    : m_data( nullptr )
    , m_size( size )
  {
    m_data = new int[ m_size ];
    for ( int i = 0; i < m_size ; ++i )
      m_data[i] = i;
  }

  ~MyArray( void )
  {
    delete [] m_data;
  }

  Iterator begin( void ) const
  {
    return Iterator( m_data );
  }

  Iterator end( void ) const
  {
    return Iterator( m_data + m_size );
  }
};

int main( void )
{
  MyArray my_array(10);

  for ( MyArray::Iterator iter = my_array.begin(); iter != my_array.end(); ++iter )
    std::cout << *iter << std::endl;

  return 0;
}
Last edited on
@football52 - Maybe this will help clear up some confusion.

At line 9, you defined a const_iterator called iter. At the moment, iter doesn't point to anything so it's not associated with inventory yet. In fact, it can be used as an iterator for any vector<string>.

At line 12, you're making the association with inventory by assigning the address of the first element of inventory (inventory.begin()) to iter. Each time through the loop iter++ causes iter to point to the next element of the vector. The for loop continues as long as iter has not reached inventory.end()
Ahhhhh thank you abstraction
Topic archived. No new replies allowed.