Skip to content
Related Articles

Related Articles

Improve Article

vector data() function in C++ STL

  • Difficulty Level : Basic
  • Last Updated : 14 Oct, 2020

The std::vector::data() is an STL in C++ which returns a direct pointer to the memory array used internally by the vector to store its owned elements. 


Parameters: The function does not accept any parameters.
Return value: The function returns a pointer to the first element in the array which is used internally by the vector.

Below program illustrate the above function:


// C++ program to demonstrate the
// vector::date() function
#include <bits/stdc++.h>
using namespace std;
int main()
    // initialising vector
    vector<int> vec = { 10, 20, 30, 40, 50 };
    // memory pointer pointing to the
    // first element
    int* pos =;
    // prints the vector
    cout << "The vector elements are: ";
    for (int i = 0; i < vec.size(); ++i)
        cout << *pos++ << " ";
    return 0;
The vector elements are: 10 20 30 40 50 
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :