Javatpoint Logo
Javatpoint Logo

How to Access Vector Elements in C++

Introduction

Due to their dynamic size and simplicity of usage, vectors are among the most frequently used data structures in C++. They provide you flexibility and speedy element retrieval by enabling you to store and retrieve items in a single, contiguous memory block. You will get a thorough grasp of how to use vectors in this tutorial as we study several ways to access vector elements in C++.

1. Accessing Elements by Index

Utilizing their indices is among the easiest methods for gaining access to vector elements. An index is assigned to each element in a vector, starting at 0 for the first element and increasing by 1 for each further member. Use the subscript operator [] and the appropriate index to retrieve an element at a given index.

Output:

First Element: 10
Third Element: 30

2. Using the at() Member Function

Using the at() member function is another technique to get at vector items. The at() method offers bounds checking to make sure you don't access elements that are larger than the vector. An std::out_of_range exception is thrown if an out-of-range index is supplied.

Output:

First Element: 10
Third Element: 30

3. Front and Back Elements

Additionally, vectors offer direct access to their first and last items via the member methods front() and rear(), respectively. When you simply need to access the vector's endpoints, these functions are quite helpful.

Output:

First Element: 10
Last Element: 50

4. Using Iterators

Iterators are a potent tool for navigating and gaining access to items in containers that are provided by C++. Iterators for vectors come in two flavors: begin() and end(). The end() iterator points one place after the last element, whereas the begin() iterator points to the start member of the vector. You may access the items of the vector by iterating over it using these iterators.

Output:

10 20 30 40 50

5. Accessing Elements with Range-Based for Loop

The range-based for loop, which streamlines the iteration process by automatically managing iterators, was introduced in C++11. Without explicitly maintaining iterators, you may access vector items by using this functionality.

Output:

10 20 30 40 50

6. Accessing Elements Using Pointers

Vectors are implemented in C++ as a dynamically created array, and pointers are used to access their elements. The data() member function may be used to get the memory address of the first element, and pointer arithmetic can be used to get the addresses of successive items.

Output:

10 20 30 40 50

7. Checking Vector Size

Verify that the vector is not empty before attempting to access any of its elements. Use the size() member function to determine a vector's size. Accessing the elements of an empty vector will result in unexpected behavior.

Output:

10 20 30 40 50

8. Modifying Vector Elements

When you have access to vector elements, you may change them in addition to retrieving their values. Using any of the access techniques, you may give vector elements new values.

Output:

15 20 35 45 55

9. Handling Out-of-Range Access

When utilizing indices to access vector elements, it's crucial to confirm that the index falls within the acceptable range. Accessing items that are larger than the vector will lead to unpredictable behavior. Make careful to carry out the necessary bounds checking if you need to access items based on computations or user input to prevent any mistakes.

Output:

Enter the index: 2
Element at index 2: 30

Conclusion

The ability to access vector elements in C++ is essential for working with this flexible data format. Understanding the different approaches-including index-based access, iterators, pointers, and the range-based for loop-will enable you to reliably obtain and modify vector items as needed for your programmer. To prevent probable problems and undefinable behavior, bear in mind to handle bounds checking, care for vector size, and apply prudence.


Next TopicStatic cast in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA