自定义的数据类型需要指定排序的方式,才可以使用set或者map容器。
set容器重载 () 的方式
需要额外定义一个类,类中重载()来指定排序的方式,Compare
中指定了排序为从大到小的排序。
#include <iostream>
#include <set>
using namespace std;
class MyData {
public:
int value;
MyData(int value) {
this->value = value;
}
};
class Compare {
public:
// 注意:参数一定要加const
bool operator()(const MyData& p1, const MyData& p2) {
return (p1.value > p2.value);
}
};
void printSet(set<MyData, Compare>& s) {
for (set<MyData>::const_iterator it = s.begin(); it != s.end(); it++) {
cout << it->value << " ";
}
cout << endl;
}
int main(int argc, char const* argv[]) {
set<MyData, Compare> s;
MyData p1(1);
MyData p2(3);
MyData p3(4);
MyData p4(2);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
printSet(s); /* 输出 4 3 2 1 */
return 0;
}
set容器重载 < 方式
如果不想额外定义类来实现指定排序,可以在原始类中直接重载<实现指定排序。
#include <iostream>
#include <set>
using namespace std;
class MyData {
public:
int value;
MyData(int value) {
this->value = value;
}
// 注意:一定要加const
bool operator<(const MyData& p) const {
return (this->value > p.value);
}
};
void printSet(set<MyData>& s) {
for (set<MyData>::const_iterator it = s.begin(); it != s.end(); it++) {
cout << it->value << " ";
}
cout << endl;
}
int main(int argc, char const* argv[]) {
set<MyData> s;
MyData p1(1);
MyData p2(3);
MyData p3(4);
MyData p4(2);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
printSet(s); /* 输出 4 3 2 1 */
return 0;
}
map容器重载 () 的方式
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Test {
public:
string name;
int age;
Test(string name, int age) {
this->name = name;
this->age = age;
}
};
class Compare {
public:
bool operator()(const Test& t1, const Test& t2) {
if (t1.age == t2.age) {
return (t1.name > t2.name);
} else {
return (t1.age > t2.age);
}
}
};
int main(int argc, char const* argv[]) {
Test t1("xiaohu", 18);
Test t2("pdd", 21);
Test t3("xiaoxiao", 23);
Test t4("kid", 20);
Test t5("khan", 20);
map<Test, int, Compare> m;
m.insert(make_pair(t1, 1));
m.insert(make_pair(t2, 2));
m.insert(make_pair(t3, 3));
m.insert(make_pair(t4, 4));
m.insert(make_pair(t5, 5));
for (map<Test, int, Compare>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "name: " << it->first.name << " age: " << it->first.age << " value: " << it->second << endl;
}
/* 输出:
name: xiaoxiao age: 23 value: 3
name: pdd age: 21 value: 2
name: kid age: 20 value: 4
name: khan age: 20 value: 5
name: xiaohu age: 18 value: 1
*/
return 0;
}
map容器重载 < 方式
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Test {
public:
Test(string name, int age) {
this->name = name;
this->age = age;
}
bool operator<(const Test& t) const {
if (this->age == t.age) {
return (this->name > t.name);
} else {
return (this->age > t.age);
}
}
string name;
int age;
};
int main(int argc, char const* argv[]) {
Test t1("xiaohu", 18);
Test t2("pdd", 21);
Test t3("xiaoxiao", 23);
Test t4("kid", 20);
Test t5("khan", 20);
map<Test, int> m;
m.insert(make_pair(t1, 1));
m.insert(make_pair(t2, 2));
m.insert(make_pair(t3, 3));
m.insert(make_pair(t4, 4));
m.insert(make_pair(t5, 5));
for (map<Test, int>::const_iterator it = m.begin(); it != m.end(); it++) {
cout << "name: " << it->first.name << " age: " << it->first.age << " value: " << it->second << endl;
}
/* 输出:
name: xiaoxiao age: 23 value: 3
name: pdd age: 21 value: 2
name: kid age: 20 value: 4
name: khan age: 20 value: 5
name: xiaohu age: 18 value: 1
*/
return 0;
}
注意
无论是重载的()还是重载的<,都一定要加const
,否则会报错。