array at() function in C++ STL
The array::at() is a built-in function in C++ STL which returns a reference to the element present at location i in given array.
Syntax:
array_name.at(i)
Parameters: The function accepts a single mandatory parameter i which specifies the location.
Return value: The function returns an element present at index i in given array if i is valid index otherwise it throws out_of_range exception.
Time Complexity: O(1)
Below programs demonstrate the array::at() function:
Program 1:
// CPP program to illustrate // the array::at() function #include <bits/stdc++.h> using namespace std; int main() { // array initialisation array<int, 5> arr = { 1, 5, 2, 4, 7 }; // prints the element at ith index // index starts from zero cout << "The element at index 2 is " << arr.at(2) << endl; return 0; } |
The element at index 2 is 2
// CPP program to illustrate // the array::at() function #include <bits/stdc++.h> using namespace std; int main() { // array initialisation array<int, 5> arr = { 1, 5, 2, 4, 7 }; // it is an exception cout << "The element at index 2 is " << arr.at(7) << endl; return 0; } |
Abort signal from abort(3) (SIGABRT)
Recommended Posts:
- array get() function in C++ STL
- 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
- Function Overloading vs Function Overriding in C++
- array::fill() and array::swap() in C++ STL
- Array with GCD of any of its subset belongs to the given array
- array::crbegin() and array::crend() in C++ STL
- array::rbegin() and array::rend() in C++ STL
- array::cbegin() and array::cend() in C++ STL
- array::front() and array::back() in C++ STL
- Sorting an array according to another array using pair in STL
- What’s difference between “array” and “&array” for “int array[5]” ?
- array::begin() and array::end() in C++ STL
- What happens when a virtual function is called inside a non-virtual function in C++
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.



