The Wayback Machine - https://web.archive.org/web/20240204103226/https://www.geeksforgeeks.org/vector-data-function-in-c-stl/
Open In App
Related Articles

vector data() function in C++ STL

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. 

Syntax: 

vector_name.data()

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.

Time Complexity – Constant O(1)

Below program illustrate the above function:

CPP




// 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 = vec.data();
 
    // prints the vector
    cout << "The vector elements are: ";
    for (int i = 0; i < vec.size(); ++i)
        cout << *pos++ << " ";
 
    return 0;
}


Output

The vector elements are: 10 20 30 40 50 

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 09 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads