array get() function in C++ STL
The
array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container.
Syntax:
get
(array_name)
Parameters: The function accepts two mandatory parameters which are described below.
- i – position of an element in the array, with 0 as the position of the first element.
- arr_name – an array container.
Return Value: The function returns a reference to the element at the specified position in the array
Time complexity: O(1)
Below programs illustrate the above function:
Program 1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
array<int, 3> arr = { 10, 20, 30 };
cout << "arr[0] = " << get<0>(arr) << "\n";
cout << "arr[1] = " << get<1>(arr) << "\n";
cout << "arr[2] = " << get<2>(arr) << "\n";
return 0;
}
|
Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
Program 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
array<char, 3> arr = { 'a', 'b', 'c' };
cout << "arr[0] = " << get<0>(arr) << "\n";
cout << "arr[1] = " << get<1>(arr) << "\n";
cout << "arr[2] = " << get<2>(arr) << "\n";
return 0;
}
|
Output:
arr[0] = a
arr[1] = b
arr[2] = c
Last Updated :
29 Aug, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...