For loop 循环完成时,FOR循环中的比较值未关联到正确的向量元素。-代码审查

For loop 循环完成时,FOR循环中的比较值未关联到正确的向量元素。-代码审查,for-loop,initialization,For Loop,Initialization,我认为我有两个问题。它们与i)初始化向量和ii)获得最小值和最大值以对应于向量中的正确值有关 我试过几件事: 当我初始化最小值和最大值时,我总是遇到这样一个问题:这两个值的初始值都是0。我试图手动输入一个值来初始化这两个变量(minimate=distance[I]),但这并没有真正起作用,因为我的向量最初是空的 #include "pch.h" #include <iostream> #include <string> #include <vector> #

我认为我有两个问题。它们与i)初始化向量和ii)获得
最小值和
最大值以对应于向量中的正确值有关

我试过几件事:

当我初始化
最小值
最大值
时,我总是遇到这样一个问题:这两个值的初始值都是
0
。我试图手动输入一个值来初始化这两个变量(
minimate=distance[I]
),但这并没有真正起作用,因为我的向量最初是空的

#include "pch.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using std::cout;
using std::cin;
using std::vector;
using std::string;

int main()
{
// Read a sequence of double values into a vector

vector <double> distance = {};                  // declaring the vector named "distance"
double sum = 0;
double smallest;
double greatest;

for (double x = 0; cin >> x;) {     // read into distance, to terminate putting values in vector use anything that is not of variable type of vector
    distance.push_back(x);                  // put distance into vector
    cout << '\n';
    for (int i = 0; i < distance.size(); i = i + 1) {   // keeping track of elements in vector by displaying them
        cout << distance[i] << '\n';
    }

}

for (int i = 0; i < distance.size(); i = i + 1) {                   // adding up all values of vector by iterating through all elements
    sum = sum + distance[i];                
    }

cout << "The total sum of all the elements in the vecotr is: " << sum << 
'\n';   



for (int i = 0; i < distance.size(); i = i + 1) {           // determining the smallest value in the vector
    if (smallest > distance[i]) {
        smallest = distance[i];
    }


}

cout << "The smallest value in the vector is: " << smallest << '\n';

for (int i = 0; i < distance.size(); i = i + 1) {     // determining the greatest value in the vector
    if (greatest < distance[i]) {
        greatest = distance[i];
    }
}

cout << "The greatest value in the vector is: " << greatest << '\n';

cout << "The mean distance between two neigbouring cities is: " << sum / distance.size() << '\n'; 
#包括“pch.h”
#包括
#包括
#包括
#包括
#包括
使用std::cout;
使用std::cin;
使用std::vector;
使用std::string;
int main()
{
//将双值序列读入向量
vector distance={};//声明名为“distance”的向量
双和=0;
双最小;
双倍最大;
对于(double x=0;cin>>x;){//读入距离,若要终止将值放入向量,请使用任何非变量类型的向量
距离。推回(x);//将距离放入向量

cout填充向量后,只需指定适当的值即可

vector <double> distance = {};      // declaring the vector named "distance"
double sum = 0;
double smallest;
double greatest;

for (double x = 0; cin >> x;) {     // read into distance, to terminate putting values in vector use anything that is not of variable type of vector
    distance.push_back(x);          // put distance into vector
    cout << '\n';
    for (int i = 0; i < distance.size(); i = i + 1) {   // keeping track of elements in vector by displaying them
        cout << distance[i] << '\n';
    }
}
if(!distance.empty()){ // if distance vector is not empty
    smallest = distance[0];
    greatest = distance[0];
}

谢谢你的建议。我可能会在我的程序中使用第一个,因为我在书中还没有达到足够的程度来使用
数值
标准库,尽管从数学上讲它对我来说更有意义…..这是否也有助于解决我在
for
循环中遇到的问题?这个建议似乎并不正确帮助在循环中写入正确的值。
#include <limits>

(...)

double smallest = std::numeric_limits<double>::max();
double greatest = std::numeric_limits<double>::min();