Share via


vector::data

Returns a pointer to the first element in the vector.

const_pointer data() const;
pointer begin();

Return Value

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

Example

// 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

Requirements

Header: <vector>

Namespace: std

See Also

Reference

vector Class

Standard Template Library