Vector Data Function - C++ Programming Tutorial
C++ Course / STL Vector / Vector Data Function

Vector Data Function

BLUF: Mastering Vector Data Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Vector Data Function

C++ is renowned for its efficiency. Learn how Vector Data Function enables low-level control and high-performance computing in the tutorial below.

C++ Vector data

This function provides a reference to an array that is internally utilized by a vector to store its elements.

Syntax

Consider a vector 'v' and pointer 'p'. The syntax for this declaration is as follows:

Example

data_type *p=v.data();

Parameter

It does not contain any parameter.

Return value

It returns a pointer to an array.

Example 1

Let's see a simple example.

Example

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v{10,20,30,40,50};
int *k=v.data();
for(int i=0;i<v.size();i++)
cout<<*k++<<" ";
return 0;
}

Output:

Output

10 20 30 40 50

In this instance, k is the integer type pointer pointing to the elements within a vector.

Example 2

Let's see a simple another example.

Example

#include <iostream>
#include<vector>
using namespace std;
int main()
{
vector<string> v{"C","C++","Java",".Net"};
string *k=v.data();
for(int i=0;i<v.size();i++)
cout<<*k++<<" ";
return 0;
}

Output:

Output

C C++ Java .Net

In this instance, k represents a string pointer that references the components of a vector denoted as v.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience