C++ max_element()

Introduction

To find the greatest element in the container, we use the std::max::element function declared in the <algorithm> header.

This function returns an iterator to the max element in the container:

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

int main() /*from  w  ww.  j a va 2s .  c om*/
{ 
    std::vector<int> v = { 1, 2, 3, 4, 5 }; 
    auto it = std::max_element(std::begin(v), std::end(v)); 
    std::cout << "The max element in the vector is: " << *it; 
} 



PreviousNext

Related