Index/Max/Min of a Vector<double> C++

I want to be able to figure out the highest and lowest element in an vector<double> and also figure out what position/index that high/low number is currently.
For example,

1
2
3
4
5
vector<double> x;
std::cout << "Enter in #s: ";
double numbers;
std::getline(std::cin, numbers);
x.push_back(numbers);


Let's say the user inputted 4.3 1.0 2.99 43.5

I would want the result to say

1
2
    The highest number is 43.5 at position 4
    The lowest number is 1.0 at position 2


I was wondering if there is any way to implement this code WITHOUT using the min_element/max_element function and do it with a for loop?

I wanted to use something like:

1
2
3
4
5
   for (int i=0;i<x.size();i++){
        if ( //the number is less than ) {
            std::cout << "The lowest number is...... at position .....";
        if ( //the number is greather than ) {
            std::cout << "The highest number is......at position......";

Something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> nums = {3, 1, 4, 2};
    
    int minPos = 0, maxPos = 0;
    for (unsigned i = 0; i < nums.size(); ++i)
    {
        if (nums[i] < nums[minPos]) // Found a smaller min
            minPos = i;
        if (nums[i] > nums[maxPos]) // Found a bigger max
            maxPos = i;
    }
    std::cout << "Min is " << nums[minPos] << " at position " << minPos;
    std::cout << "\nMax is " << nums[maxPos] << " at position " << maxPos;
}

Output:
Min is 1 at position 1
Max is 4 at position 2

If you look at the reference pages for min_element/max_element, they basically give you a sample implementation that you can refer to.
http://www.cplusplus.com/reference/algorithm/min_element/
http://www.cplusplus.com/reference/algorithm/max_element/
Topic archived. No new replies allowed.