我认为这是一个非常基本的问题,但我无法弄明白.
我习惯于在C++中使用数组,但我现在开始学习矢量.我正在制作一个测试代码,我遇到了一个问题.
首先,这是我制作的代码:
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main(){
vector<double> score(10);
for(vector<double>::size_type i=0;i<20;i++) {
cout<<"Enter marks for student #"<<i+1<<":"<<flush;
cin>>score[i];
}
double total = accumulate(score.begin(), score.end(),0);
cout<<"Total score:"<<total<<endl<<"Average score:"<<total/score.size()<<flush;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在for第9行的句子中,我宣称i是一种vector<double>::size_type类型(因为我被告知要这样做).我测试了上面用上面所说的类型取代的代码int,它工作得非常好.为什么比较vector<double>::size_type优先int?