Access the elements of a vector through an iterator. : vector elements « vector « C++ Tutorial






#include <iostream>
#include <vector>
#include <cctype>
using namespace std;
   
int main()
{
  vector<char> v(10); // create a vector of length 10
  vector<char>::iterator p; // create an iterator
  int i;
   
  // assign elements in vector a value
  p = v.begin();
  i = 0;
  while(p != v.end()) {
    *p = i + 'a';
    p++;
    i++;
  }
   
  // display contents of vector
  cout << "Original contents:\n";
  p = v.begin();
  while(p != v.end()) {
    cout << *p << " ";
    p++;
  }
  cout << "\n\n";
   
  // change contents of vector
  p = v.begin();
  while(p != v.end()) {
    *p = toupper(*p);
    p++;
  }
   
  // display contents of vector
  cout << "Modified Contents:\n";
  p = v.begin();
  while(p != v.end()) {
    cout << *p << " ";
    p++;
  }
  cout << endl;
   
  return 0;
}








16.26.vector elements
16.26.1.Vector Creation and Element Access
16.26.2.Change element with pointer
16.26.3.Change element with const iterator
16.26.4.Accessing Elements in a Vector Using Array Semantics
16.26.5.Accessing Elements in a Vector Using Pointer Semantics (Iterators)
16.26.6.Set Element Values in a vector Using Array Semantics
16.26.7.Instantiate a vector with 10 elements (it can grow larger)
16.26.8.Instantiate a vector with 10 elements, each initialized to 90
16.26.9.Instantiate a vector to 5 elements taken from another
16.26.10.The first and last element in vector as pointed to by begin() and end() - 1
16.26.11.The first and last element in vector as pointed to by rbegin() and rend() - 1
16.26.12.Add elements to the end of vector with push_back
16.26.13.Constructing a Container That Has Identical Elements
16.26.14.Constructing a Container That Has Specified Values
16.26.15.Elements Being Created, Assigned, and Destroyed
16.26.16.Add all the elements from vector Two to the end of vector One
16.26.17.append one more element. This causes reallocation
16.26.18.Instantiate a vector with 4 elements, each initialized to 90
16.26.19.Access the elements of a vector through an iterator.