
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Array::operator[] Function
The C++ std::array::operator[] function provides a access to elements of a array by index. It allows both read and write access, similar to the traditional arrays. This operator takes an index as its argument and returns a reference to the element at that position.
Unlike at() method, operator[] does not perform bounds checking, so accessing a out of range element results in undefined behaviour.
Syntax
Following is the syntax for std::array::operator[]() function.
reference operator[] (size_type n); const_reference operator[] (size_type n) const;
Parameters
- n − It indicates the position of an element in the array.
Return Value
It return a reference to the element at the specified location in the array.
Exceptions
This function never throws exception if value of n is valid array index, otherwise behaviour is undefined.
Time complexity
Constant i.e. O(1)
Example 1
Let's look at the following example, where we are going to access and modify the element.
#include <iostream> #include <array> int main() { std::array < int, 5 > a = {1,22,23,34,45}; std::cout << "Element at given index : " << a[3] << std::endl; a[3] = 33; std::cout << "After modification : " << a[3] << std::endl; return 0; }
Output
Output of the above code is as follows −
Element at given index : 34 After modification : 33
Example 2
Consider the following example, where we are going to use the operator[] in the loop.
#include <iostream> #include <array> int main() { std::array < int, 4 > a = {10,11,12,23}; for (int x = 0; x < a.size(); ++x) { std::cout << "Element at index " << x << ": " << a[x] << std::endl; } return 0; }
Output
Following is the output of the above code −
Element at index 0: 10 Element at index 1: 11 Element at index 2: 12 Element at index 3: 23
Example 3
In the following example, we are going to access the element that is out of range and observing the output.
#include <iostream> #include <array> int main() { std::array < int, 3 > x = {12,23,34}; std::cout << "Element at given index: " << x[5] << std::endl; return 0; }
Output
If we run the above code it will generate the following output −
Element at given index: -1233708736