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:
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.
#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:
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.
#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:
C C++ Java .Net
In this instance, k represents a string pointer that references the components of a vector denoted as v.