Mega Code Archive

 
Categories / C++ Tutorial / Vector
 

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