C++,为什么这里push_back没能存入容器?
关于自己写一个类的基础小练习,功能是读入学生姓名,期中期末成绩,作业成绩(不指定作业次数),然后计算出学生最终成绩并输出。我分步运行后发现,record可以存入我输入的内...
关于自己写一个类的基础小练习,功能是读入学生姓名,期中期末成绩,作业成绩(不指定作业次数),然后计算出学生最终成绩并输出。
我分步运行后发现,record可以存入我输入的内容,但在push_back到students里时就会失败,程序直接跳过了这一步,然后查看vector里是空的,size=0。请问是哪里出了问题?
代码:
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <iomanip>
using namespace std;
class student_info
{
public:
string name() const { return n; }
bool valid() const { return !homework.empty(); }
istream& read(istream&);
double grade() const;
private:
string n;
double midterm, final;
vector<double> homework;
};
bool compare(const student_info&a, const student_info& b)
{
return a.name() > b.name();
}
istream& read_hw(istream& in, vector<double>& hw)
{
if (in)
{
hw.clear();
double x;
while (in >> x)
{
hw.push_back(x);
in.clear();
}
return in;
}
}
istream& student_info::read(istream& in)
{
cout << "input name mid fin" << endl;
in >>n >> midterm >> final;
cout << "input hw" << endl;
read_hw(in, homework);
return in;
}
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_sz;
vec_sz size = vec.size();
if (size == 0)
{
throw domain_error("median of an empty vector");
}
sort(vec.begin(), vec.end());
vec_sz mid = size / 2;
return size & 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
}
double grade(double midterm, double final, double homework)
{
return 0.2*midterm + 0.4*final + 0.4*homework;
}
double grade(const double midterm, const double final, const vector<double>& hw)
{
if (hw.size() == 0)
{
throw domain_error("student has done no homework !");
}
return grade(midterm, final, median(hw));
}
double student_info::grade() const
{
return ::grade(midterm, final, homework);
}
int main()
{
vector<student_info> students;
student_info record;
string::size_type maxlen = 0;
while (record.read(cin))
{
maxlen = max(maxlen, record.name().size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare);
for (vector<student_info>::size_type i = 0; i != students.size(); i++)
{
cout << students[i].name() << string(maxlen + 1 - students[i].name().size(), ' ');
try
{
double final_grade = students[i].grade();
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec) << endl;
}
catch (domain_error e)
{
cout << e.what() << endl;
}
}
system("pause");
return 0;
} 展开
我分步运行后发现,record可以存入我输入的内容,但在push_back到students里时就会失败,程序直接跳过了这一步,然后查看vector里是空的,size=0。请问是哪里出了问题?
代码:
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <stdexcept>
#include <iomanip>
using namespace std;
class student_info
{
public:
string name() const { return n; }
bool valid() const { return !homework.empty(); }
istream& read(istream&);
double grade() const;
private:
string n;
double midterm, final;
vector<double> homework;
};
bool compare(const student_info&a, const student_info& b)
{
return a.name() > b.name();
}
istream& read_hw(istream& in, vector<double>& hw)
{
if (in)
{
hw.clear();
double x;
while (in >> x)
{
hw.push_back(x);
in.clear();
}
return in;
}
}
istream& student_info::read(istream& in)
{
cout << "input name mid fin" << endl;
in >>n >> midterm >> final;
cout << "input hw" << endl;
read_hw(in, homework);
return in;
}
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_sz;
vec_sz size = vec.size();
if (size == 0)
{
throw domain_error("median of an empty vector");
}
sort(vec.begin(), vec.end());
vec_sz mid = size / 2;
return size & 2 == 0 ? (vec[mid] + vec[mid - 1]) / 2 : vec[mid];
}
double grade(double midterm, double final, double homework)
{
return 0.2*midterm + 0.4*final + 0.4*homework;
}
double grade(const double midterm, const double final, const vector<double>& hw)
{
if (hw.size() == 0)
{
throw domain_error("student has done no homework !");
}
return grade(midterm, final, median(hw));
}
double student_info::grade() const
{
return ::grade(midterm, final, homework);
}
int main()
{
vector<student_info> students;
student_info record;
string::size_type maxlen = 0;
while (record.read(cin))
{
maxlen = max(maxlen, record.name().size());
students.push_back(record);
}
sort(students.begin(), students.end(), compare);
for (vector<student_info>::size_type i = 0; i != students.size(); i++)
{
cout << students[i].name() << string(maxlen + 1 - students[i].name().size(), ' ');
try
{
double final_grade = students[i].grade();
streamsize prec = cout.precision();
cout << setprecision(3) << final_grade << setprecision(prec) << endl;
}
catch (domain_error e)
{
cout << e.what() << endl;
}
}
system("pause");
return 0;
} 展开
1个回答
展开全部
push_back,算法语言里面的一个函数名,如c++中的vector头文件里面就有这个push_back函数,在vector类中作用为在vector尾部加入一个数据。
string中也有这个函数,作用是字符串之后插入一个字符。
函数原型
voidpush_back(value_type_Ch);
参数
_Ch-->Thecharactertobeaddedtotheendofthestring.
在vector类中:
voidpush_back(const_Ty&_X)
{
insert(end(),_X);
}
在vector<_Bool, _Bool_allocator>类中:
voidpush_back(constbool_X)
{
insert(end(),_X);
}
string中也有这个函数,作用是字符串之后插入一个字符。
函数原型
voidpush_back(value_type_Ch);
参数
_Ch-->Thecharactertobeaddedtotheendofthestring.
在vector类中:
voidpush_back(const_Ty&_X)
{
insert(end(),_X);
}
在vector<_Bool, _Bool_allocator>类中:
voidpush_back(constbool_X)
{
insert(end(),_X);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询