c++ find minimum value in vector

To find the minimum value in a vector in C++, you can follow these steps:

  1. Include the necessary header file: cpp #include <vector>

  2. Declare a vector variable and initialize it with some values: cpp std::vector<int> numbers = {5, 2, 8, 1, 9};

  3. Declare a variable to store the minimum value and initialize it with the first element of the vector: cpp int minValue = numbers[0];

  4. Iterate over the vector and compare each element with the current minimum value: cpp for (int i = 1; i < numbers.size(); i++) { if (numbers[i] < minValue) { minValue = numbers[i]; } }

  5. At the end of the loop, the variable minValue will contain the minimum value in the vector.

Here's the complete code:

#include <vector>

int main() {
    std::vector<int> numbers = {5, 2, 8, 1, 9};
    int minValue = numbers[0];

    for (int i = 1; i < numbers.size(); i++) {
        if (numbers[i] < minValue) {
            minValue = numbers[i];
        }
    }

    // minValue now contains the minimum value in the vector
    return 0;
}

You can modify the code according to your specific needs, such as changing the data type of the vector or using a different comparison operator for finding the minimum value.