Accessing Elements in a Vector Using Pointer Semantics (Iterators) : vector elements « vector « C++ Tutorial






#include <iostream>
#include <vector>

int main ()
{
    using namespace std;

    vector <int> v;

    v.push_back (50);
    v.push_back (1);
    v.push_back (987);
    v.push_back (1001);

    // Access objects in a vector using iterators:
    vector<int>::iterator i = v.begin();

    while (i != v.end ())
    {
        size_t nElementIndex = distance (v.begin (),
                      i);

        cout << "Element at position ";
        cout << nElementIndex << " is: " << *i << endl;

        // move to the next element
        ++ i;
    }

    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.