⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 《C++ STL开发技术引导》配套光盘 聆听大师级的指点
💻 CPP
字号:
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

struct Student{
	int id;
	char* name;
	int score;
	Student(int id_, char* name_, int score_){
		id=id_;
		name=name_;
		score=score_;
	}
};

bool compByid(Student s1, Student s2){
	return s1.id < s2.id ? 1 : 0;
}
bool compByscore(Student s1, Student s2){
	return s1.score < s2.score ? 1 : 0;
}	

void print(Student s){
	cout << s.id << ' ' << s.name << ' ' << s.score << endl;
}

int main(void){
	vector<Student> v;
	v.push_back(Student(5, "李强", 90));
	v.push_back(Student(9, "王文", 80));
	v.push_back(Student(8, "张天", 87));
	v.push_back(Student(6, "丁宏", 90));
	v.push_back(Student(7, "赵庆", 99));
	//
	cout << "按学号执行sort算法排序:\n";
	sort(v.begin(), v.end(), compByid);
	for_each(v.begin(), v.end(), print);
	cout << endl ;
	//
	cout << "按分数执行stable_sort算法排序:\n";
	stable_sort(v.begin(), v.end(), compByscore);
	for_each(v.begin(), v.end(), print);
	cout << endl;
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -