C++STL算法 is_sorted比较学生成绩

总分和裸分其实没差那么多,也就是100一里

#include<iostream>
#include<cstdlib>
#include<vector>
#include<algorithm>

using namespace std;

enum AddColumn
{
	AddType_A = 5,// 班级*
	AddType_B = 50,// 领导亲戚
	AddType_C = 3,// 校队队员
	AddType_D = -15,// 违规违纪
	AddType_E = 0// 一般选手
};

class Stu
{
public:
	typedef int index;
	explicit Stu(index point , AddColumn add):point(point), add(add)
	{
		index temp= point + add;
		if (temp > 100)temp = 100;
		if (temp < 0)temp = -1;
		this->total = temp;
	}
	bool operator<(Stu s) { return this->total < s.total; }
	friend bool compareStu(Stu s1, Stu s2);
	operator index() { return this->total; }
	//index getpoint() { return this->point; }
private:
	index point;//裸分
	AddColumn add;// 加分
	index total;// 总分
};

bool compareStu(Stu s1, Stu s2) { return s1.point < s2.point; }

int main()
{
	vector<Stu> stuVec;
	stuVec.push_back(Stu(99, AddType_E));
	stuVec.push_back(Stu(48, AddType_B));
	stuVec.push_back(Stu(100, AddType_D));
	stuVec.push_back(Stu(81, AddType_C));
	sort(stuVec.begin(), stuVec.end(),less<Stu::index>());// 总分从小到大排列
	for_each(stuVec.begin(), stuVec.end(), [](Stu s) {cout << Stu::index(s) << "\t"; });
	cout << endl;

	bool result = is_sorted(stuVec.begin(), stuVec.end(), compareStu);
	if (result)
	{
		cout << "NB的人在NA和NC之间徘徊" << endl;
	}
	else
	{
		cout << "公平一直是相对的" << endl;
	}
	//for_each(stuVec.begin(), stuVec.end(), [](Stu s) {cout << s.getpoint() << "\t"; });
	
	return EXIT_SUCCESS;
}
上一篇:Java-Stream流方法学习及总结


下一篇:java 新特性之 stream 流