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 parameter to the function.
Syntax:
array_name.at(position) Parameters: Position of the element to be fetched.
Return: It return the reference to the element at the given position.
Examples:
Input: array_name1 = [1, 2, 3]
array_name1.at(2);
Output: 3
Input: array_name2 = ['a', 'b', 'c', 'd', 'e']
array_name2.at(4);
Output: e
// CPP program to illustrate // Implementation of at() function #include <bits/stdc++.h> using namespace std; int main() { // Take any two array array<int, 3> array_name1; array<char, 5> array_name2; // Inserting values for (int i = 0; i < 3; i++) array_name1[i] = i+1; for (int i = 0; i < 5; i++) array_name2[i] = 97+i; // Printing the element cout << "Element present at position 2: " << array_name1.at(2) << endl; cout << "Element present at position 4: " << array_name2.at(4); return 0; } |
Output:
Element present at position 2: 3 Element present at position 4: e
Time Complexity: Constant i.e. O(1).
Application:
Given an array of integers, print integers in alternate fashion starting from 1st position.
Input: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Output: 2 4 6 8 10
// CPP program to illustrate // application of at() function #include <bits/stdc++.h> using namespace std; int main() { // Declare array array<int, 10> a; // Inserting values for (int i = 0; i < 10; i++) a[i] = i+1; for (int i = 0; i < a.size(); ++i) { if (i % 2 != 0) { cout << a.at(i); cout << " "; } } return 0; } |
Output:
2 4 6 8 10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Implementation of lower_bound() and upper_bound() in Array of Pairs in C++
- Difference between cout and std::cout in C++
- Sorting Vector of Arrays in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Difference between Array and Map
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Trigraphs in C++ with Examples
- Difference Between DART and C++
- How to fix auto keyword error in Dev-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.

