![]() |
VOOZH | about |
In C++, the vector operator [] is used to randomly access or update the elements of vector using their indexes. It is similar to the vector at() function but it doesn't check whether the given index lies in the vector or not.
Let’s take a look at a simple code example:
3
Explanation: The third element in the vector v is 3, which is accessed using the operator[].
This article covers the syntax, usage, and common examples of vector operator [] in C++:
Table of Content
v[i];
Parameters:
Return Value:
The vector operator[] is simple and widely used. The code examples below show how to use it:
1 2 3 5 4
1 2 3 10 4
132049
The output of this program is undefined and depends on the system or compiler. It may print garbage values or cause a program crash (segmentation fault).