position of max element in vector c++

Finding the Position of the Max Element in a Vector in C++

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

int main() {
    std::vector<int> vec = {10, 20, 30, 40, 50};
    auto maxElement = std::max_element(vec.begin(), vec.end());
    int position = std::distance(vec.begin(), maxElement);
    std::cout << "The maximum element is " << *maxElement << " at position " << position << std::endl;
    return 0;
}
  1. Include necessary header files.
  2. Define the main function.
  3. Create a vector of integers.
  4. Use std::max_element to find the iterator to the maximum element in the vector.
  5. Calculate the position of the maximum element using std::distance.
  6. Output the maximum element and its position.