Vector begin() in C++ STL
Last Updated :
26 Nov, 2024
In C++, the vector begin() is a built-in method used to obtain an iterator pointing to the start of the vector. This iterator is used to traverse the vector or perform operations starting from the beginning of the vector.
Let’s take a look at an example that illustrates the use of the vector begin() method:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 2, 4};
// Printing first element using vector begin
cout << *(v.begin());
return 0;
}
Explanation: The begin() method returns an iterator pointing to the first element of the vector, which can then be dereferenced to access the element.
This article covers the syntax, usage, and common examples of the vector begin() method in C++:
Syntax of Vector begin()
The vector begin() is a member method of the std::vector class defined inside the <vector> header file.
v.begin();
Parameters:
- This function does not take any parameter.
Return Value:
- Returns an iterator pointing to the starting element of the vector.
Supported Iterator Operations
The vector begin() returns an iterator of type vector::iterator, which is a random-access iterator. It supports all operations allowed in iterator arithmetic, such as dereferencing, incrementing/decrementing, adding/subtracting integers, subtraction of another iterator, and comparison.
However, just like pointers, iterators can point to invalid memory location. So, make sure that the iterator is valid before using.
Examples of Vector begin()
The vector begin() function is used to perform various operations on a vector using iterators. The following examples illustrate its usage:
Iterator Over a Vector
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 2, 4};
// Use begin() to iterate through the vector
for (auto it = v.begin(); it != v.end(); ++it)
cout << *it << " ";
return 0;
}
Explanation: The iterator returned by vector begin() is incremented and dereferenced until it equals the vector end() iterator, marking the end of the vector.
Access Element at a Given Index
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 2, 4};
// Access element at index 3
cout << *(v.begin() + 3);
return 0;
}
Explanation: The begin() iterator points to the first element, and indexing starts from 0. So, adding the index to the begin() iterator will move it to point to the element at the given index.
Find the Index of an Element
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 2, 4};
int val = 5;
// Finding the iterator to the element val
auto it = find(v.begin(), v.end(), val);
// Finding the index of val
cout << it - v.begin();
return 0;
}
Explanation: The find() function returns the iterator to the given element in the vector. This iterator can be subtracted from vector begin() to find the index of the element.
Find the Sum of Vector Elements
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {1, 3, 5, 2, 4};
// Sort vector v using sort()
cout << accumulate(v.begin(), v.end(), 0);
return 0;
}
Explanation: Almost all STL algorithms operate on ranges defined by iterators. The begin() and end() methods are used to define the range for the entire vector for accumulate() to work on.
Similar Reads
Vector crbegin() in C++ STL
In C++, the vector crbegin() is a built-in method used to obtain a constant reverse iterator pointing to the last element of the vector. It is used to mark the starting point of reverse iteration over the vector without modifying its elements. Letâs take a look at an example: [GFGTABS] C++ #include
2 min read
Vector assign() in C++ STL
In 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. [GFGTABS] C++ #include
4 min read
Vector back() in C++ STL
In C++, the vector back() is a built-in function used to retrieve the last element of the vector. It provides a reference to the last element which allows us to read or modify it directly. Letâs take a quick look at a simple example that illustrates the vector back() method: [GFGTABS] C++ #include
3 min read
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
8 min read
vector::at() in C++ STL
In C++, vector at() is a built-in method used to access an element in a vector using index. It is the only access method that performs bound checking before accessing the element to confirm whether the given index lies is within the vector. Letâs take a quick look at a simple example that uses vecto
3 min read
Vector end() in C++ STL
In 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 illust
4 min read
Vector pop_back() in C++ STL
In C++, the vector pop_back() is a built-in method used to remove the last element from a vector. It reduces the size of the vector by one, but the capacity remains unchanged. Letâs take a look at an example that illustrates the vector pop_back() method: [GFGTABS] C++ #include <bits/stdc++.h>
3 min read
Vector clear() in C++ STL
In C++, vector clear() is a built-in method used to remove all elements from a vector, making it empty. In this article, we will learn about the vector clear() method in C++. Letâs take a look at an example that illustrates the vector clear() method: [GFGTABS] C++ #include <bits/stdc++.h> usin
2 min read
Vector crend() in C++ STL
In 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: [GFGTABS] C++ #include <bits/stdc++.h>
2 min read
Vector insert() in C++ STL
In 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: [GFGTABS] C++ #include <bits/stdc++.
5 min read