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,

 ``12345`` ``````vector 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

 ``12`` `````` 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:

 ``12345`` `````` for (int i=0;i

Something like this?
 ``123456789101112131415161718`` ``````#include #include int main() { std::vector 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.