Cycle through vector in the forward direction using an iterator. - C++ STL

C++ examples for STL:vector

Description

Cycle through vector in the forward direction using an iterator.

Demo Code

#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<int> vect);
int main() {/*from  w  ww.j  a  v a  2 s.c o m*/
   vector<int> v(10);
   for(unsigned i=0; i < v.size(); ++i)
      v[i] = i*i;
   show("Contents of v: ", v);
   vector<int>::iterator itr;
   vector<int>::reverse_iterator ritr;
   cout << "Cycle through the vector in the forward direction:\n";
   for(itr = v.begin(); itr != v.end(); ++itr)
      cout << *itr << " ";
   cout << "\n\n";
   cout << "Now, use a reverse iterator to cycle through in the"  << " reverse direction:\n";
   return 0;
}
void show(const char *msg, vector<int> vect) {
   cout << msg;
   for(unsigned i=0; i < vect.size(); ++i)
      cout << vect[i] << " ";
   cout << "\n";
}

Result


Related Tutorials