所以我不得不寫入n個實數的矢量,然後打印最大的。C++矢量下標超出範圍線1201
#include <iostream>
#include <vector>
using namespace std;
void printMax(vector<double>);
int main()
{
vector<double> vct;
double n;
while(cin >> n)
vct.push_back(n);
printMax(vct);
return 0;
}
void printMax(vector<double> x)
{
int max;
for(int i = 1; i < x.size(); i++)
{
if(x[i] > x[i - 1]) max = x[i];
else continue;
}
cout << "Max = \t" << max << endl;
}
當我啓動該程序它讓我輸入數字,但只要我按ctrl + z和進入崩潰,並說:矢量標超出範圍線:1201我認爲一個問題是與無效printMax
部分。
它代表別的東西:) @LightnessRacesinOrbit –