Export (0) Print
Expand All

vector::data

Returns a pointer to the first element in the vector.

const_pointer data() const;
pointer begin();

A pointer to the first element in the vector Class or to the location succeeding an empty vector.

// vector_data.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main()
{
    using namespace std;
    vector<int> c1;
    vector<int>::pointer c1_Ptr;
    vector<int>::const_pointer c1_cPtr;

    c1.push_back(1);
    c1.push_back(2);

    cout << "The vector c1 contains elements:";
    c1_cPtr = c1.data();
    for (size_t n = c1.size(); 0 < n; --n, c1_cPtr++)
    {
        cout << " " << *c1_cPtr;
    }
    cout << endl;

    cout << "The vector c1 now contains elements:";
    c1_Ptr = c1.data();
    *c1_Ptr = 20;
    for (size_t n = c1.size(); 0 < n; --n, c1_Ptr++)
    {
        cout << " " << *c1_Ptr;
    }
    cout << endl;
}
The vector c1 contains elements: 1 2
The vector c1 now contains elements: 20 2

Header: <vector>

Namespace: std

Show:
© 2014 Microsoft