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:
// CPP program to demonstrate the // array::get() function #include <bits/stdc++.h> using namespace std; int main() { // array initialisation array< int , 3> arr = { 10, 20, 30 }; // function call cout << "arr[0] = " << get<0>(arr) << "\n" ; cout << "arr[1] = " << get<1>(arr) << "\n" ; cout << "arr[2] = " << get<2>(arr) << "\n" ; return 0; } |
arr[0] = 10 arr[1] = 20 arr[2] = 30
Program 2:
// CPP program to demonstrate the // array::get() function #include <bits/stdc++.h> using namespace std; int main() { // array initialisation array< char , 3> arr = { 'a' , 'b' , 'c' }; // function call cout << "arr[0] = " << get<0>(arr) << "\n" ; cout << "arr[1] = " << get<1>(arr) << "\n" ; cout << "arr[2] = " << get<2>(arr) << "\n" ; return 0; } |
arr[0] = a arr[1] = b arr[2] = c
Recommended Posts:
- array at() function in C++ STL
- Sum 2D array in Python using map() function
- getline() function and character array
- How to return a local array from a C/C++ function?
- Declare a C/C++ function returning pointer to array of integer pointers
- array::fill() and array::swap() in C++ STL
- Sorting an array according to another array using pair in STL
- array::front() and array::back() in C++ STL
- Array with GCD of any of its subset belongs to the given array
- array::rbegin() and array::rend() in C++ STL
- array::crbegin() and array::crend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- What’s difference between “array” and “&array” for “int array[5]” ?
- Function Overloading vs Function Overriding in C++
- array::begin() and array::end() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.