vector data() function in C++ STL
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector< int > vec = { 10, 20, 30, 40, 50 };
int * pos = vec.data();
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
Last Updated :
09 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...