小编cof*_*low的帖子

C++:"vector <int> :: size_type variable" - 以这种方式声明的重点是什么?

我认为这是一个非常基本的问题,但我无法弄明白.

我习惯于在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

c++ types vector std

16
推荐指数
2
解决办法
2452
查看次数

标签 统计

c++ ×1

std ×1

types ×1

vector ×1