STL vector按多字段值排序

转自:http://blog.csdn.net/pathuang68/article/details/7526381

上一篇我们讲到STL map的key如果由多个值组成,并按照这些值分别进行排序的情况。在最后的结论中我们说到“通常我们不用STL algorithm中的sort函数,来对一个map进行排序,而对vector的元素进行排序则可以很方面地使用sort函数。

下面就是一个完整的,vector按多字段值进行排序的示例代码:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;  

class Student
{
private:
	int id;			// 学号
	string name;		// 姓名
	float eyesight;		// 视力
	float height;		// 身高
	float chinese;		// 语文成绩
	float english;		// 英文成绩
	float math;		// 数学成绩
public:
	Student(int id, string name, float eyesight, float height, float chinese, float english, float math)
	{
		this->id = id;
		this->name = name;
		this->eyesight = eyesight;
		this->height = height;
		this->chinese = chinese;
		this->english = english;
		this->math = math;
	}

	int get_id()
	{
		return id;
	}

	string get_name()
	{
		return name;
	}

	float get_eyesight()
	{
		return eyesight;
	}

	float get_height()
	{
		return height;
	}

	float get_chinese()
	{
		return chinese;
	}

	float get_english()
	{
		return english;
	}

	float get_math()
	{
		return math;
	}
};

// 比较大小的函数(谓词)
// bool comparer(Student& stu_a, Student& stu_b)在gcc下error : invalid initialization of reference of type ‘Student&’ from expression of type ‘const Student’ 常引用const无法初始化
 bool comparer(Student stu_a, Student stu_b)
{
	// 按eyesight升序 + height升序排列
	if(stu_a.get_eyesight() != stu_b.get_eyesight())	
		return (stu_a.get_eyesight() < stu_b.get_eyesight());
	else
		return (stu_a.get_height() < stu_b.get_height());

	// 按eyesight降序° + height降序排列
	//if(stu_a.get_eyesight() != stu_b.get_eyesight())	
	//	return (stu_a.get_eyesight() > stu_b.get_eyesight());
	//else
	//	return (stu_a.get_height() > stu_b.get_height());

	// 按eyesight升序 + height降序排列
	//if(stu_a.get_eyesight() != stu_b.get_eyesight())	
	//	return (stu_a.get_eyesight() < stu_b.get_eyesight());
	//else
	//	return (stu_a.get_height() > stu_b.get_height());

	// 按eyesight降序 + height升序排列
	//if(stu_a.get_eyesight() != stu_b.get_eyesight())	
	//	return (stu_a.get_eyesight() > stu_b.get_eyesight());
	//else
	//	return (stu_a.get_height() < stu_b.get_height());
}

int main(int argc, char** argv)
{
	vector<Student> vec;
	Student student(4, "Dudley", 1.1f, 170.2f, 90.5f, 89.5f, 93.0);
	vec.push_back(student);
	Student student2(3, "Chris", 1.1f, 163.4f, 93.5f, 90.0f, 83.5f);
	vec.push_back(student2);
	Student student3(2, "Bob", 1.5f, 166.6f, 86.0f, 98.5f, 85.0f);
	vec.push_back(student3);
	Student student4(1, "Andrew", 1.5f, 173.2f, 98.5f, 100.0f, 100.f);
	vec.push_back(student4);

	// 调用STL中的sort函数,其中的第三个参数就是我们前面定义的,比较两个Student对象大小的函数
	sort(vec.begin(), vec.end(), comparer);

	vector<Student>::iterator iter;
	for(iter = vec.begin(); iter != vec.end(); ++iter)
	{
		cout << (*iter).get_eyesight() << "\t" << (*iter).get_height()<<"\t"<<(*iter).get_name() << endl;
	}

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL vector是C++标准模板库(STL)中的容器之一,用于存储和管理动态大小的元素序列。它提供了一些便利的操作和功能,比如自动调整大小、快速随机访问、插入和删除元素等。 你可以使用vector来存储任何类型的元素,包括基本数据类型和自定义对象。vector使用连续的内存来存储元素,并支持在常量时间内访问任意位置的元素。 下面是一些常用的vector操作: 1. 创建vector: 可以使用默认构造函数或将现有的序列作为参数传递给构造函数来创建vector对象。 ```cpp #include <vector> std::vector<int> numbers; // 声明一个空的vector std::vector<int> numbers = {1, 2, 3, 4}; // 使用初始列表创建vector ``` 2. 访问元素: 可以使用下标运算符([])来访问vector中的元素。 ```cpp std::vector<int> numbers = {1, 2, 3, 4}; int firstElement = numbers[0]; // 访问第一个元素 int lastElement = numbers[numbers.size() - 1]; // 访问最后一个元素 ``` 3. 添加和删除元素: 可以使用push_back()函数将元素添加到vector的末尾,并使用pop_back()函数删除末尾的元素。 ```cpp std::vector<int> numbers; numbers.push_back(1); // 在末尾添加元素 numbers.pop_back(); // 删除末尾的元素 ``` 4. 大小操作: 可以使用size()函数获取vector中元素的数量,并使用empty()函数检查vector是否为空。 ```cpp std::vector<int> numbers = {1, 2, 3, 4}; int size = numbers.size(); // 获取元素数量 bool isEmpty = numbers.empty(); // 检查是否为空 ``` 5. 迭代器: 可以使用迭代器遍历vector中的元素。 ```cpp std::vector<int> numbers = {1, 2, 3, 4}; for (auto it = numbers.begin(); it != numbers.end(); ++it) { std::cout << *it << " "; // 输出每个元素 } ``` 这些只是vector的一些基本操作,STL vector还提供了许多其他功能和算法,可以根据需求进行使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值