#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;
}
【C++】vector、list、set、map容器在自定义类型时进行排序以及map按照实值排序
于 2024-04-14 11:51:16 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)