Use const_iterator to loop through the vector : vector iterator « Vector « C++






Use const_iterator to loop through the vector

   
 

#include <iostream>
using std::cout;
using std::endl;

#include <vector>
using std::vector;

int main()
{
   int array[ 6 ] = { 1, 2, 3, 4, 5, 6 }; // initialize array
   vector< int > integers; // create vector of ints

   integers.push_back( 2 );
   integers.push_back( 3 );
   integers.push_back( 4 );

   vector< int >::const_iterator constIterator;

   // display vector elements using const_iterator
   for ( constIterator = integers.begin();
      constIterator != integers.end(); ++constIterator )
      cout << *constIterator << ' ';

   cout << endl;
   return 0;
}

/* 
2 3 4

 */        
    
    
  








Related examples in the same category

1.Display contents of vector through an iterator
2.Change contents of vector through an iterator
3.interator operator for vector
4.Loop through all elements in a vector in reverse order by using rbegn, rend
5.vector.begin, vector.end returns the iterators
6.const_iterator from different containers