![]() |
In C++, vectors are dynamic arrays that were introduced to replace the traditional arrays. In this article, we’ll learn how to access the first element of a vector in C++. Example Input: myVector = {5, 1, 8, 3, 4} Output: First element of myVector is: 5 Access the First Element of a Vector in C++The std::vector class has the std::vector::front() member function which provides a simple way to access the first element of the vector. We can also simply access the first element using its index which is 0 and [] operator. C++ Program to Access the First Element in a VectorC++
Output
The first element using front(): 1 The first element using myVector[0]: 1 Time Complexity: O(1), as vector provide random access using index. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |