【C++】vector、list、set、map容器在自定义类型时进行排序以及map按照实值排序

本文介绍了如何在C++中使用`vector`,`list`,`set`,和`map`容器,并展示了如何自定义排序规则,通过`my_Compare`仿函数或函数对象实现对容器内Person对象的年龄和姓名排序。
摘要由CSDN通过智能技术生成
#include<iostream>
#include<algorithm>//仅随机访问迭代器可以使用
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
class Person {//测试输入容器的数据
public:
	Person(string name, int age){
		this->m_Age = age;
		this->m_Name=name;
	}
	string m_Name;
	int m_Age;
};
class my_Compare {//规定的排序规则仿函数
public:
	bool operator()(const Person& p1, const Person& p2) const {//按照TRUE的方式排序
		if (p1.m_Age == p2.m_Age) {
			return p1.m_Name < p2.m_Name;
		}
		return p1.m_Age < p2.m_Age;
	}
};
bool myCompare(Person& p1, Person& p2) {//规定的排序方式函数,按照TRUE的方式排序
	if (p1.m_Age == p2.m_Age) {
		return p1.m_Name < p2.m_Name;
	}
	return p1.m_Age < p2.m_Age;
}
void test_Vector() {
	Person p1("Aik", 5);
	Person p2("Emb", 10);
	Person p3("Boq", 8);
	Person p4("Cql", 10);
	Person p5("Dlp", 15);
	vector<Person> vp;
	vp.push_back(p1);
	vp.push_back(p2);
	vp.push_back(p3);
	vp.push_back(p4);
	vp.push_back(p5);
	cout << "vector排序前" << endl;
	for (vector<Person>::iterator it = vp.begin(); it != vp.end(); it++) {
		cout << "姓名--" << it->m_Name << "  " << "年龄--" << it->m_Age << endl;
	}
	cout << "===============" << endl;
	cout << "vector排序后" << endl;
	sort(vp.begin(), vp.end(),myCompare);//对vector进行排序,传入排序规则函数
    //sort(vp.begin(), vp.end(),my_Compare());也可以使用仿函数
	for (vector<Person>::iterator it = vp.begin(); it != vp.end(); it++) {
		cout << "姓名--" << it->m_Name << "  " << "年龄--" << it->m_Age << endl;
	}
}
void test_List() {//测试容器list
	Person p1("Aik", 5);
	Person p2("Emb", 10);
	Person p3("Boq", 8);
	Person p4("Cql", 10);
	Person p5("Dlp", 15);	
	list<Person> lp;//构造list
	lp.push_back(p1);
	lp.push_back(p2);
	lp.push_back(p3);
	lp.push_back(p4);
	lp.push_back(p5);
	cout << "list排序前" << endl;
	for (list<Person>::iterator it = lp.begin(); it != lp.end(); it++) {
		cout << "姓名--" << it->m_Name <<"  " << "年龄--" << it->m_Age << endl;
	}
	cout << "===============" << endl;
	cout << "list排序后" << endl;
	lp.sort(myCompare);//对list进行排序,传入排序规则函数
    //lp.sort(my_Compare());也可以使用仿函数
	for (list<Person>::iterator it = lp.begin(); it != lp.end(); it++) {
		cout << "姓名--" << it->m_Name << "  " << "年龄--" << it->m_Age << endl;
	}
}
void test_Set() {//测试set
	Person p1("Aik", 5);
	Person p2("Emb", 10);
	Person p3("Boq", 8);
	Person p4("Cql", 10);
	Person p5("Dlp", 15);
	set<Person,my_Compare> sp;//构造set,set插入后自动排序,自定义数据要传入仿函数
	sp.insert(p1);
	sp.insert(p3);
	sp.insert(p2);
	sp.insert(p5);
	sp.insert(p4);
	for (set<Person>::iterator it = sp.begin(); it != sp.end(); it++) {
		cout << "姓名--" << it->m_Name << "  " << "年龄--" << it->m_Age << endl;
	}
}
void test_Map() {//map中所有数据元素均为队组pair类型,且默认按照键值排序
	map<int,Person> mp;
	Person p1("Aik", 5);
	Person p2("Emb", 10);
	Person p3("Boq", 8);
	Person p4("Cql", 10);
	Person p5("Dlp", 15);
	mp.insert(make_pair(1, p1));
	mp.insert(make_pair(2, p2));
	mp.insert(make_pair(3, p3));
	mp.insert(make_pair(4, p4));
	mp.insert(make_pair(5, p5));
	for (map<int, Person>::iterator it = mp.begin(); it != mp.end(); it++) {
		cout << "姓名--" << it->second.m_Name << "  " << "年龄--" << it->second.m_Age << endl;
	}
	cout << "===============" << endl;
	cout << "map按照实值排序" << endl;
	list<Person> lip;//借助其他容器进行排序
	for (map<int, Person>::iterator it = mp.begin(); it != mp.end(); it++) {
		lip.push_back(it->second);
	}
	lip.sort(myCompare);
	for (list<Person>::iterator it = lip.begin(); it != lip.end(); it++) {
		cout << "姓名--" << it->m_Name << "  " << "年龄--" << it->m_Age << endl;
	}
}
int main() {
	test_Vector();
	cout << "===============" << endl;
	test_List();
	cout << "===============" << endl;
	cout << "set自动排序" << endl;
	test_Set();
	cout << "===============" << endl;
	cout << "map自动排序" << endl;
	test_Map();
	return 0;
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值