gpt4 book ai didi

c++ - 加速 C++(第 3.2.1 节 : vector homework)

转载 作者:太空宇宙 更新时间:2023-11-03 10:42:28 25 4
gpt4 key购买 nike

<分区>

在《Accelerated C++》一书第 42 页的 3.2.1 - 将数据集合存储在 vector 中 部分,我在按照代码告诉我输入的内容后遇到了代码错误.

// revised version of the excerpt
double x;
vector<double> homework;

// invariant: homework contains all the homework grades read so far
while (cin >> x)
homework.push_back(x);

我理解 vector 的概念,但我只是不明白为什么我的代码会给我一条错误消息,特别指向

vector<double> homework;

声明。 C++11 和 C++14 是否不再支持这种 vector 声明?

这是我的确切代码:

#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <ios>
#include <string>

using std::cin; using std::string;
using std::cout; using std::setprecision;
using std::endl; using std::streamsize;

int main()
{
// ask for and read the student's name
cout << "\n Please enter your first name: ";
string name;
cin >> name;
cout << " Hello, " << name << "!" << endl;

// ask for and read the midterm and final grades
cout << " Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;

// ask for the homework grades
cout << " Enter all your homework grades, "
" followed by end-of-file: ";

//the number and sum of grades read so far
int count = 0;
double sum = 0;

// a variable into which to read
double x;
vector<double> homework;

/*invariant:
we have read COUNT grades so far, and
SUM is the sum of the first COUNT grades*/
while (cin >> x) {
homework.pushback(x);
}

// write the result
streamsize prec = cout.precision();
cout << " Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;

return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com