sort排序不能用

C++语言 码拜 8年前 (2016-09-20) 1385次浏览
请教一下,如下的代码中,调用了sort 排序,vector中为什么没有按总成绩大小排序:

#ifndef __TEST__H
#define __TEST__H
#include <iostream>
#include <vector>
using namespace std;
class Student
{
public:
//构造函数
	Student(const string &strName, const string &iSchoolNbr , const double iChinese, const double iMath, const double Math, const double English, const double History): \
		m_Name(strName), m_SchoolNbr(iSchoolNbr), m_MathScore(iMath), m_ChineseScore(iChinese), m_EngScore(English), m_HistoryScore(History), m_TotalScore(0), \
		m_AverageScore(0)
	{
		m_TotalScore = m_MathScore + m_ChineseScore + m_EngScore + m_HistoryScore;
		m_AverageScore = m_TotalScore / mCourseNbr;
	}
	Student(): m_Name(""), m_SchoolNbr(""), m_MathScore(0), m_ChineseScore(0), m_EngScore(0), m_HistoryScore(0), m_TotalScore(0), m_AverageScore(0)
	{
	}
//输入,输出操作符
	friend ostream & operator << (ostream &Out, const Student &stu1);
	friend istream & operator >> (istream &In, Student &stu1);
//小于操作符
	friend bool  comp (const Student &stu1, const Student &stu2);
	Student & operator = (const Student &stu);
private:
	static int mCourseNbr;
	string m_Name;
	string m_SchoolNbr;
	double m_MathScore;
	double m_ChineseScore;
	double m_EngScore;
	double m_HistoryScore;
	double m_TotalScore;
	double m_AverageScore;
};
int Student::mCourseNbr = 4;
#endif
#include "test.h"
ostream & operator << (ostream &Out, const Student &stu1)
{
	Out << "shool number: " << stu1.m_SchoolNbr << endl;
	Out << "Name: " << stu1.m_Name << endl;
	Out << "Chinese Score: " << stu1.m_ChineseScore << endl;
	Out << "Math Score: " << stu1.m_MathScore << endl;
	Out << "English Score: " << stu1.m_EngScore << endl;
	Out << "History Score: " << stu1.m_HistoryScore << endl;
	Out << "Total Score: " << stu1.m_TotalScore << endl;
	Out << "Average Score: " << stu1.m_AverageScore << endl;
	return Out;
}
istream & operator >> (istream &In, Student &stu1)
{
	In >> stu1.m_SchoolNbr >> stu1.m_Name >> stu1.m_ChineseScore >> stu1.m_MathScore >> stu1.m_EngScore >> stu1.m_HistoryScore;
	stu1.m_TotalScore = stu1.m_ChineseScore + stu1.m_MathScore + stu1.m_EngScore + stu1.m_HistoryScore;
	stu1.m_AverageScore = stu1.m_TotalScore / stu1.mCourseNbr;
	return In;
}
bool comp (const Student &stu1, const Student &stu2)
{
	if (stu1.m_TotalScore < stu2.m_TotalScore)
		return 1;
	else
		return 0;
}
Student & Student::operator = (const Student &stu)
{
	if (this == &stu)
		return *this;
	else
	{
		m_SchoolNbr = stu.m_SchoolNbr;
		m_Name = stu.m_Name;
		m_ChineseScore = stu.m_ChineseScore;
		m_MathScore = stu.m_MathScore;
		m_EngScore = stu.m_EngScore;
		m_HistoryScore = stu.m_HistoryScore;
		m_TotalScore = stu.m_TotalScore;
		m_AverageScore = stu.m_AverageScore;
		return *this;
	}
}
int main()
{
	vector< class Student> MyVec;
	while (1)
	{
		Student stu2;
		cout << "Please input: school nbr,name, chinese score, math score, english score, history score" << endl;
		cin >> stu2;
		MyVec.push_back(stu2);
		string strKey("");
		cout << "if end, please input end" << endl;
		cin >> strKey;
		if (strKey == "end")
			break;
	}
	sort(MyVec.begin(), MyVec.end(), comp);
	vector<class Student>::const_iterator Iter = MyVec.begin();
	while (Iter != MyVec.end())
	{
		cout << *Iter++;
		cout << "--" << endl;
	}
	return 0;
}
解决方案

40

加了头文件吗?algorithm

20

经过本人的测试,能正常排序
由于编译不过,修改了这些
1加头文件
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int Student::mCourseNbr = 4; 这语句从.h文件移到.cpp文件中
输入的数据顺序是 nbr1 nbr2 nbr3
sort排序不能用

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明sort排序不能用
喜欢 (0)
[1034331897@qq.com]
分享 (0)