Get the first and last element from vector using rbegin() and rend() - C++ STL

C++ examples for STL:vector

Description

Get the first and last element from vector using rbegin() and rend()

Demo Code

#include <iostream>
#include <vector>
using namespace std;
void show(const char *msg, vector<int> vect);
int main() {//w w w . j  a  va2 s  .com
   vector<int> v(10);
   for(unsigned i=0; i < v.size(); ++i)
      v[i] = i*i;
   show("Contents of v: ", v);
   cout << "The first and last element in v as"
   << " pointed to by rbegin() and rend()-1:\n"
   << *v.rbegin() << ", " << *(v.rend()-1) << "\n\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