array data() in C++ STL with Examples
The array::data() is a built-in function in C++ STL which returns an pointer pointing to the first element in the array object.
Syntax:
array_name.data()
Parameters: The function does not accept any parameters.
Return Value: The function returns an pointer.
Below programs illustrate the above function:
Program 1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
array< int , 5> arr = { 1, 2, 3, 4, 5 };
cout << "The array elements are: " ;
for ( auto it = arr.begin(); it != arr.end(); it++)
cout << *it << " " ;
auto it = arr.data();
cout << "\nThe first element is:" << *it;
return 0;
}
|
Output:
The array elements are: 1 2 3 4 5
The first element is:1
Program 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
array< int , 5> arr = { 1, 2, 3, 4, 5 };
cout << "The array elements are: " ;
for ( auto it = arr.begin(); it != arr.end(); it++)
cout << *it << " " ;
auto it = arr.data();
it++;
cout << "\nThe second element is: " << *it;
it++;
cout << "\nThe third element is: " << *it;
return 0;
}
|
Output:
The array elements are: 1 2 3 4 5
The second element is: 2
The third element is: 3
Last Updated :
23 Jul, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...