Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;:&引用;向量<;int>;::大小“U型变量”这样声明有什么意义?_C++_Vector_Types_Std - Fatal编程技术网

C++ C++;:&引用;向量<;int>;::大小“U型变量”这样声明有什么意义?

C++ C++;:&引用;向量<;int>;::大小“U型变量”这样声明有什么意义?,c++,vector,types,std,C++,Vector,Types,Std,我认为这是一个非常基本的问题,但我无法理解 我习惯于在C++中使用数组,但现在我开始学习向量。 我在编写测试代码时遇到了一个问题 首先,以下是我编写的代码: #include <iostream> #include <vector> #include <numeric> using namespace std; int main(){ vector<double> score(10); for(vector<double>:

我认为这是一个非常基本的问题,但我无法理解

我习惯于在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;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
向量评分(10分);

对于(vector::size_type i=0;i,
vector
size_type
是vector用来进行大小比较的。如果有一个循环使用
int
作为计数器,并与向量的实际大小进行比较,则编译器会发出关于有符号与无符号整数比较的警告:

for( int i=0; i<score.size(); i++ ){  // <-- signed vs. unsigned comparisons
    // do something...
}

for(int i=0;i嵌入式类型
vector::size_type
与各种
vector
方法的返回值相关,例如
.size()
,因此在这种情况下首选使用匹配类型(
int=0
通常会导致符号不匹配警告)

补充说明:

与基于索引的循环相比,更喜欢基于迭代器的循环:

for (vector<double>::iterator i = score.begin(); i != score.end(); ++i) {
  // ...
}

或者甚至对每个
使用一个带有lambda的

大小类型
保证足够大,可以容纳支持的最大向量大小,
向量::max_size()
int
不是:在许多常见平台上,
int
有32位,而
max_size()
大大大于231位


如果你知道尺寸是(并且永远是)一个像20这样的小数字,那么你可以使用
int
或任何其他整数类型而不是
size\u-type
。如果你要更改程序,例如从输入中读取大小,那么如果该值大于
int\u MAX
,就会出现可怕的错误;而使用
size\u-type
,则会失败t使用最大值
max_size()
,您可以轻松测试它。

您的问题是双重的

首先,您在
std::vector
的末尾之外进行写入--
std::vector
有10个元素,您正在写入20个元素

为了解决这个问题,并遵循“不要重复自己”原则,您需要按如下方式更改代码:

int main(){
  std::vector<double> score(20);

  for(std::vector<double>::size_type i=0;i<score.size();i++) {
我们得到一个有符号/无符号的比较(可能):

std::size\u t
中定义为一个副词)

如果您使用C++11编程,您可以做得更好:

int i=0;
for(double& student:score) {
  std::cout<<"Enter marks for student #"<<++i<<":"<<std::flush;
  std::cin>>student;
}
inti=0;
对于(双倍和学生:分数){

std::couty当前代码超出了向量的界限。如果将20更改为
score.size()
,你应该得到一个关于有符号和无符号比较的警告。顺便说一句,你对
刷新
的调用是不必要的,因为在读取输入之前会刷新输出。我总是使用
std::size\u t
。我认为这是允许的。如果没有,那么我就有一个下午的时间进行重构!@Bathsheba,我知道的所有实现都是
>std::size\u t
类型。当然,可能有一个没有。@chris我认为它存在,因为分配器可以更改容器的
size\u-type
的类型。但有一个不可忽略的可能性,我只是想出来。对不起,伙计们,20是我的错误,它是10。我不知道为什么我这么做了,它出了问题你的远程代理的主体。@quentin它只需要更多的呼吸空间。嘿,它现在看起来确实更好了。但由于缺少声明的
i
;)而无法编译)是否有任何特定的理由不使用
std::size_t
,即
std::vector
size_type
不仅仅是
std::size_t
的别名的任何特定情况?我总是使用
std::size_t
进行数组/向量/任何索引和大小表示,因为它应该是标准的/可移植的pe为此目的。@Manu343726:标准不保证
size\u类型
将是
std::size\u t
。但在实践中,总是这样。因此,使用
std::size\u t
还是
size\u类型
取决于“纯净”程度你想遵守标准。我个人觉得
std::vector::size\u type
太长了。它实际上很长,但这不是typedef和C++11“类型别名”想要简化的吗?使用它实际上是免费的:)
for (double& i : score) {
  // ...
}
int main(){
  std::vector<double> score(20);

  for(std::vector<double>::size_type i=0;i<score.size();i++) {
int main(){
  std::vector<double> score(20);

  for(int i=0;i<score.size();i++) {
i<score.size()
  for(std::size_t i=0;i<score.size();i++) {
int i=0;
for(double& student:score) {
  std::cout<<"Enter marks for student #"<<++i<<":"<<std::flush;
  std::cin>>student;
}