How to find the maximum element of a Vector using STL in C++?



A vector in C++ is a dynamic array that stores elements of the same data type and can change its size when needed. In this article, we are given a vector and our goal is to find the maximum (largest) element using different STL methods in C++.

Let's understand this with an example:

// Example 1
std::vector<int> vec1 = {11, 13, 21, 45, 8};
The largest element is 45. 


// Example 2
std::vector<int> vec2 = {1, 9, 2, 5, 7};
The largest element is 9.

Finding Maximum Element of a Vector Using STL in C++

There are multiple ways to find the largest element in a vector in C++. We will cover the following approaches using STL functions:

Using std::max_element()

In this approach, we use the std::max_element() function from the <algorithm> header. This function looks at all the elements in a given range and returns the iterator to the largest one. We will use this function to find the maximum value in the vector.

Example

Below is a C++ program where we create a vector and pass its begin() and end() iterators to std::max_element() and it returns the position of the largest value which we then print.

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {4, 9, 2, 6, 3};

    // Print all elements in the vector
    std::cout << "Vector elements: ";
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    // Find the maximum element using std::max_element
    auto maxElem = std::max_element(vec.begin(), vec.end());
    // Print the maximum element
    std::cout << "The maximum element is: " << *maxElem << std::endl;
    return 0;
}

Below is the output of the program, which shows the largest number in the given vector using the minmax_element() function:

Vector elements: 4 9 2 6 3 
The maximum element is: 9

Time Complexity: O(n) because std::max_element() checks each element once to find the maximum.

Space Complexity: O(1) because no extra storage beyond a few variables is used.

Using std::minmax_element()

The std::minmax_element() function from the STL finds both the smallest and largest elements in a range in a single pass. It returns a pair of iterators where .first points to the minimum element and .second points to the maximum element. We use .second to get the maximum value.

Example

Here is a C++ program where we pass a vector's begin() and end() iterators to std::minmax_element() and print the value pointed to by the second iterator, which is the maximum element.

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {4, 9, 2, 6, 3};

    // Display all elements of the vector
    std::cout << "Vector elements: ";
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Find the min and max element using std::minmax_element
    auto result = std::minmax_element(vec.begin(), vec.end());
    // Display the maximum element
    std::cout << "The maximum element is: " << *result.second << std::endl;
    return 0;
}

The output below shows the vector elements and the maximum value found using the minmax_element() function.

Vector elements: 4 9 2 6 3 
The maximum element is: 9

Time Complexity: O(n), we scan the vector once to find both the minimum and maximum elements.

Space Complexity: O(1), the space used is constant.

Using std::sort()

The std::sort() function from the STL sorts elements in a given range. We use it here with std::greater<int>() as a comparator to sort the vector in descending order. This way, the largest element comes first.

Example

Here is a complete C++ program where we sort the vector in descending order and then print the first element as the maximum value.

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {4, 9, 2, 6, 3};

    // Display all elements of the vector
    std::cout << "Vector elements: ";
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Sort the vector in descending order
    std::sort(vec.begin(), vec.end(), std::greater<int>());

    // Display the maximum element (first element after sorting)
    std::cout << "The maximum element is: " << vec[0] << std::endl;
    return 0;
}

Here's the output of the program. It first shows the vector elements and then prints the largest one after sorting in descending order

Vector elements: 4 9 2 6 3 
The maximum element is: 9

Time Complexity: O(n log n) because std::sort sorts the entire vector.

Space Complexity: O(log n) because std::sort uses quicksort which requires O(log n) stack space.

Using a priority queue

In this approach, we use std::priority_queue from the STL, which by default behaves like a max heap. This means the largest element always stays at the top, and we can directly access it using the top() function to get the maximum value.

Example

Here is a complete C++ program where we insert all vector elements into a priority queue and then print the top element as the maximum value.

#include <iostream>
#include <vector>
#include <queue>

int main() {
    std::vector<int> vec = {4, 9, 2, 6, 3};

    // Display all elements of the vector
    std::cout << "Vector elements: ";
    for (int num : vec) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    // Create a max heap (priority queue)
    std::priority_queue<int> pq(vec.begin(), vec.end());

    // Display the maximum element
    std::cout << "The maximum element is: " << pq.top() << std::endl;
    return 0;
}

Below is the output showing the largest number from the vector using a max-heap priority queue.

Vector elements: 4 9 2 6 3 
The maximum element is: 9

Time Complexity: O(n log n) becuase inserting each element into the priority queue takes O(log n).

Space Complexity: O(n), we store all elements in the priority queue.

Updated on: 2025-08-19T17:15:15+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements