Stackoverflow 太狗屎了
我目前正在阅读《编程原理》一书。它向我展示了一些矢量示例,例如以下代码:
#include "std_lib_facilities.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector <double> temps;
double temp = 0;
double sum = 0;
double high_temp = 0;
double low_temp = 0;
while (cin >> temp)
temps.push_back(temp);
for (int i = 0; i<temps.size(); ++i)
{
if (temps[i] > high_temp) high_temp = temps[i];
if (temps[i] < low_temp) low_temp = temps[i];
sum += temps[i];
}
cout << "High temperature: " << high_temp << endl;
cout << "Low temperature: " << low_temp << endl;
cout << "Average temperature: " << sum / temps.size() << endl;
return 0;
}
我不知道当它使用
temps[i]
将 int i = 0
放在括号内时意味着什么。那么,它说明了什么?我希望我已经说清楚了。
temps[i]
表示您正在尝试访问向量中的第“i”个元素。所以如果 i=0
那么 temps[i]
就意味着 temps[0]
。