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

vector data() function in C++ STL

Last Updated : 09 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
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 

Previous Article
Next Article

Similar Reads

vector::front() and vector::back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::front() This function can be used to fetch the first element of a vector container.Syntax : vectorname.front() Parameters : No value is needed to pass as
3 min read
vector::empty() and vector::size() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::empty() The empty() function is used to check if the vector container is empty or not.Syntax : vectorname.empty() Parameters : No parameters are p
5 min read
vector::push_back() and vector::pop_back() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::push_back() push_back() function is used to push elements into a vector from the back. The new value is inserted into the vector at the end, after the cur
4 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
vector::at() and vector::swap() in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::at() at() function is used reference the element present at the position given as the parameter to the function. Syntax: vectorname.at(position) Parameter
4 min read
vector::begin() and vector::end() in C++ STL
Vectors are the same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::begin() begin() function is used to return an iterator pointing to the first element of the vector container. begin() function returns a bidirectional
3 min read
vector :: cbegin() and vector :: cend() in C++ STL
Vectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterator cannot modify the contents of the vector. Synta
2 min read
Initializing Vector using an Existing Vector in C++ STL
A vector is a type of container which can store objects of similar data type. Vector acts like a dynamic array where we can insert elements and the size of the array increases depending upon the elements inserted. Syntax: vector&lt;data_structure/type&gt; vector_name(size, item) To know more about vectors refer to vectors in C++. The 3D vector in C
5 min read
vector rbegin() and rend() function in C++ STL
vector::rbegin() is a built-in function in C++ STL which returns a reverse iterator pointing to the last element in the container. Syntax: vector_name.rbegin() Parameters: The function does not accept any parameter.Return value: The function returns a reverse iterator pointing to the last element in the container.Program to demonstrate the vector::
2 min read
vector emplace() function in C++ STL
The vector::emplace() is an STL in C++ which extends the container by inserting a new element at the position. Reallocation happens only if there is a need for more space. Here the container size increases by one.Syntax: template iterator vector_name.emplace (const_iterator position, element); Parameter: The function accepts two mandatory parameter
3 min read
Article Tags :
Practice Tags :