the use of the subscripting operator : vector access « vector « C++ Tutorial






#include <iostream>
#include <vector>

using namespace std;

void show(const char *msg, vector<int> vect);

int main() {

  // Declare a vector that has an initial capacity of 10.
  vector<int> v(10);

  for(unsigned i=0; i < v.size(); ++i) v[i] = i*i;

  show("Contents of v: ", v);

  // the use of the subscripting operator.
  int sum = 0;
  for(unsigned i=0; i < v.size(); ++i) sum += v[i];
  double avg = sum / v.size();
  cout << "The average of the elements is " << avg << "\n\n";

  return 0;
}

// Display the contents of a vector<int>.
void show(const char *msg, vector<int> vect) {
  cout << msg;
  for(unsigned i=0; i < vect.size(); ++i)
    cout << vect[i] << " ";
  cout << "\n";
}








16.25.vector access
16.25.1.Random access of a vector
16.25.2.the use of the subscripting operator
16.25.3.Checked and Unchecked Access of a Vector
16.25.4.access field for vector
16.25.5.Bidirectional random access iterators