Vector operator[ ] in C++ STL Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 5, 4}; // Access the 3rd element using operator[] cout << v[2]; return 0; } Output3Explanation: 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 ContentSyntax of Vector operator[]Example of Vector operator[]Access Elements in VectorModify Elements in VectorOut-of-Bound Access Using [] OperatorSyntax of Vector operator[]v[i];Parameters:i: The zero-based index of the element. (position in the sequency starting from 0)Return Value:Returns a reference to the element at given index.If the given index is out of the bound, then may lead to segmentation fault.Example of Vector operator[]The vector operator[] is simple and widely used. The code examples below show how to use it:Access Elements in Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 5, 4}; // Access elements using operator[] for (size_t i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } Output1 2 3 5 4 Modify Elements in Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 5, 4}; // Update the 4th element to 10 v[3] = 10; for (size_t i = 0; i < v.size(); i++) cout << v[i] << " "; return 0; } Output1 2 3 10 4 Out-of-Bound Access Using [] Operator C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 2, 3, 4, 5}; // Accessing an element out of range cout << v[6] << endl; return 0; } Output132049 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). Comment More infoAdvertise with us Next Article Vector front() in C++ STL A AyushSaxena Follow Improve Article Tags : C++ STL cpp-vector cpp-containers-library Practice Tags : CPPSTL Similar Reads Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read 8 Ways to Initialize Vector in C++ Initializing a vector means assigning some initial values to the std::vector elements. In this article, we will learn 8 different ways to initialize a vector in C++.Table of ContentUsing Initializer ListOne by One InitializationWith a Single ValueFrom an ArrayFrom Another VectorFrom Any STL Containe 5 min read Commonly Used MethodsVector end() in C++ STLIn C++, the vector end() is a built-in method used to obtain an iterator pointing to the theoretical element after the last element of the vector. Even though this iterator does not point to a valid element, it serves as a marker for the end of the vector.Letâs take a look at an example that illustr 3 min read Vector empty() in C++ STLIn C++, vector empty() is a built-in method used to check whether the given vector is empty or not. In this article, we will learn about vector empty() method in C++.Letâs take a look at an example that illustrates the vector empty() method:C++#include <bits/stdc++.h> using namespace std; int 2 min read Vector operator[ ] in C++ STLIn 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:C++#include <bits/stdc++.h 3 min read Vector front() in C++ STLIn C++, the vector front() is a built-in function used to retrieve the first element of the vector. It provides a reference to the first element, allowing you to read or modify it directly.Letâs take a quick look at a simple example that uses the vector front() method:C++#include <bits/stdc++.h 2 min read Vector push_back() in C++ STLIn C++, the vector push_back() is a built-in method used to add a new element at the end of the vector. It automatically resizes the vector if there is not enough space to accommodate the new element.Letâs take a look at an example that illustrates the vector push_back() method:C++#include <bits/ 2 min read Vector insert() in C++ STLIn C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Letâs take a look at an example that shows the how to use this function:C++#include <bits/stdc++.h> using 4 min read vector emplace() in C++ STLIn C++, vector emplace() is used to insert elements at the given position in a vector by constructing it in-place within the vector, rather than creating a temporary object and then moving or copying it into the vector.Let's take a quick look at a simple example that uses vector emplace() method:C++ 3 min read Vector assign() in C++ STLIn C++, the vector assign() is a built-in method used to assign the new values to the given vector by replacing old ones. It also modifies the size of the vector according to the given number of elements. Letâs take a look at an example that shows the how to use this function.C++#include <bits/st 4 min read Vector erase() in C++ STLIn C++, vector erase() is a built-in function that is used to delete elements from the vector. It removes an element of a specific position or range of elements from the vector. Letâs take a simple example that uses the vector erase() method:C++#include <bits/stdc++.h> using namespace std; int 3 min read Other Member MethodsVector max_size() in C++ STLIn C++, vector max_size() is a built-in function used to find the maximum number of elements that can be held by the vector container. In this article, we will learn about the vector max_size() function in C++.Let's take a look at an example that illustrates this method:CPP#include <bits/stdc++.h 3 min read Vector capacity() in C++ STLIn C++, the vector capacity() is a built-in method used to find the capacity of vector. The capacity indicates how many elements the vector can hold before it needs to reallocate additional memory. In this article, we will learn about vector capacity() method in C++.Letâs take a look at an example t 3 min read vector rbegin() and rend() Functions in C++ STLIn C++, std::vector::rbegin() and std::vector::rend() are built-in functions used to retrieve reverse iterators to the reverse beginning and reverse end of a vector. These functions allow easy traversal of vectors in reverse i.e. from the last element to the first. They are the member functions of t 3 min read vector :: cbegin() and vector :: cend() in C++ STLVectors are known as dynamic arrays which can change its size automatically when an element is inserted or deleted. This storage is maintained by container. vector::cbegin() The function returns an iterator which is used to iterate container. The iterator points to the beginning of the vector.Iterat 2 min read Vector crend() in C++ STLIn C++, the vector crend() is a built-in method used to obtain a constant reverse iterator pointing to the theoretical element just before the first element of the vector. It is used to mark the reverse end of the vector.Letâs take a look at an example: C++#include <bits/stdc++.h> using namesp 2 min read Vector resize() in C++ STLIn C++, the vector resize() is a built-in method used to change the size of vector container after it is declared. It can be used to increase or decrease the size of vector.Letâs take a look at an example that illustrates the vector resize() method:C++#include <bits/stdc++.h> using namespace s 3 min read Vector shrink_to_fit() in C++ STLIn C++, vector shrink_to_fit() is a built-in function used to reduce the capacity of the vector to fit its size and destroys all elements beyond the size. In this article we will learn about vector shrink_to_fit() in C++.Letâs take a quick look at an example that illustrates vector shrink_to_fit() m 2 min read When to use Vector reserve() in C++?In a vector, when the number of elements to be inserted are greater than the current capacity, it is reallocated to a larger memory block and all the items are copied to this new block making this reallocation an expensive operation with time complexity of O(n).This mechanism works well if you don't 2 min read Vector data() in C++ STLIn C++, the vector data() is a built-in function used to access the internal array used by the vector to store its elements. In this article, we will learn about vector data() in C++.Letâs take a look at an example that illustrates the vector data() method:C++#include <bits/stdc++.h> using nam 2 min read 2D Vector in C++ A 2D vector is a vector of the vectors i.e. each element is a vector in itself. It can be visualised as a matrix where each inner vector represents a row, and the number of rows represents the maximum columns. A 2D vector is dynamically resizable in both dimensions.SyntaxC++vector<vector<data_ 7 min read Passing Vector to a Function in C++ To perform operations on vector belonging to one function inside other function, we pass this vector to the function as arguments during the function call.C++ provides three methods to pass a vector to a function in C++. Let's look at each of them one by one.Table of ContentPass Vector by ValuePass 5 min read How Does a Vector Internally Works in C++? In C++, a vector is a dynamic array that can resize itself when more elements are added or deleted. So, one may ask that how a vector internally works to achieve its dynamic resizing capability while maintaining similar efficiency to static arrays in operations?For vector to work as a resizable dyna 5 min read How to implement our own Vector Class in C++? The given task is to implement a class in C++ which behaves just like the Vector class.Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements a 5 min read Advantages of Vector Over Array in C++ In C++, both vectors and arrays are used to store collections of elements, but vector offers significant advantages over arrays in terms of flexibility, functionality, and ease of use. This article explores the benefits of using vectors in C++ programming.The major advantages of vector over arrays a 3 min read Common Vector ProgramsSorting a Vector in C++Sorting a vector means arranging the elements of vector in ascending order or in descending order or in desired defined order. In this article, we will learn about different ways to sort the vector in C++.The most efficient way to sort the vector is by using sort() method. Letâs take a look at an ex 3 min read How to Reverse a Vector using STL in C++?Reversing the vector means the swapping last element and first element, second last element and second element, and so on. In this article, we will learn how to reverse a vector using STL in C++.The most efficient method to reverse the vector is by using reverse() function. Letâs take a look at a si 3 min read How to Find the Minimum and Maximum Element of a Vector Using STL in C++?In this article, we will learn how to find the minimum and maximum element in vector in C++.The simplest method to find the minimum and maximum element in vector is by using min_element() and max_element(). Letâs take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; 2 min read How to Find Index of a Given Element in a Vector in C++?Vectors stores elements in contiguous memory and these elements can be accessed by their indexes. In this article, we will learn the reverse process, i.e., finding the index of the given element in a vector in C++.The simplest way to find the index of the given element in the vector is by using find 3 min read Like