array at() function in C++ STL (original) (raw)
Last Updated : 13 Jun, 2022
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 `
// 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;
}
`
Output:
The element at index 2 is 2
Program 2 : Illustrating function when it is implemented on lesser size array causing an error.
CPP `
// 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 7 is " << arr.at(7) << endl;
return 0;
}
`
Output:
Abort signal from abort(3) (SIGABRT)
Similar Reads
- 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 2 min read
- Pass Array to Functions in C++ In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code. In C++, an array can be passed in a function using a poi 5 min read
- Pass Array to Functions in C Passing an array to a function allows the function to directly access and modify the original array. In this article, we will learn how to pass arrays to functions in C.In C, arrays are always passed to function as pointers. They cannot be passed by value because of the array decay due to which, whe 3 min read
- any_of() Function in C++ STL any_of() is the C++ function defined in library in STL. This function determines whether even one element in a given range satisfies a specified criterion. If at least one element meets the property, then it returns true; otherwise, it returns false. Also if the range is empty then 2 min read
- array::at() in C++ STL Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::at() This function is used to return the reference to the element present at the position given as the par 2 min read
- strol() function in C++ The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such characte 3 min read
- std::function in C++ The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s 5 min read
- copy_n() Function in C++ STL Copy_n() is the C++ function defined in library in STL. It helps to copy one array element to the new array. Copy_n function allows the freedom to choose how many elements must be copied in the destination container. This function takes 3 arguments, the source array name, the size 2 min read
- list back() function in C++ STL The list::back() function in C++ STL returns a direct reference to the last element in the list container. This function is different from the list::end() function as the end() function returns only the iterator to the last element. Syntaxlist_name.back();ParametersThis function does not accept any 1 min read
- array::size() in C++ STL The array::size() method is used to find the number of elements in the array container. It is the member method std::array class defined inside header file. In this article, we will learn about the array::size() method in C++.Example:C++// C++ Program to illustrate the use of array::si 2 min read