Open In App

Vector begin() in C++ STL

Last Updated : 26 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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;
}

Output
1

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;
}

Output
1 3 5 2 4 

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;
}

Output
2

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;
}

Output
2

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;
}

Output
15

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.


Next Article
Practice Tags :

Similar Reads