加速C++(第 3.2.1 节:矢量<double>作业)
Accelerated C++ (Section 3.2.1: vector<double> homework)
在《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<double> homework;
声明。c++ 11和c++ 14不再支持向量的这种声明了吗?
这是我的确切代码:
#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;
}
std::vector
位于标题<vector>
中。为了使用它,头需要包含在您的代码中。要做到这一点,你需要#include <vector>
。然后,您还需要有一个using std::vector;
或使用std::vector
。
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- C++在数学计算中将double转换为int
- 在c++中为double类型的数组创建一个unique_ptr
- vector<vector<double>> to mxArray using memcpy
- (double) 和 double() 之间的区别
- C++标准是否允许<double>在没有开销的情况下实现 std::可选
- C++ Version Of Double.longBitsToDouble
- 错误:类型"double()"和"double()"的操作数无效到二进制&quo
- 有没有办法在C++的赋值中将"char**"转换为"double"?
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 使用 std::vector<double> 访问由 std::unique_ptr<double[2] 管理的数据>
- 将 std::vector<double> 从 C++ 包装到 C 以在 Swift 中使用
- 错误:无法将"<lambda(double)>"转换为"double(*)(double)"
- 如何正确将"strings"转换为"double"?
- 如何在<double>矢量中创建复杂类型的模板<T>?
- "double* grade"、"double *grade"和"double* fn()"有什么区别?
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ