sort/map/unordered_map自定义类型如何构造比较函数
来源:互联网 发布:命令行启动mysql 编辑:程序博客网 时间:2024/06/02 01:49
sort
: 定义比较函数 / 定义比较类,用比较类定义对象 map
: 比较类 / 比较函数在自定义类中提供 unordered_map
: hash类的定义、 ==运算符重载
注:/代表或, 、代表并
比较函数
bool compare(const) const
比较类的定义
struct cmp { bool operator()(const ) const };
hash函数的定义
struct hashKey { size_t operator()(const) const };
具体实现代码
#include <iostream>#include <random>#include <vector>#include <map>#include <array>#include <unordered_map>#include <algorithm>#include <random>#include <functional>#include <time.h>#include <string>using namespace std;const unsigned arraySize = 10;typedef struct student { bool operator < (const student& r) const { return age < r.age; } bool operator==(const student& r) const { return age == r.age && name == r.name; } student() { } student(string n, int a) : name(n), age(a) { } string name; int age;}student;typedef struct cmpLess{ bool operator()(const student& l, const student& r) const { return l.age < r.age; }}cmpLess; /*typedef struct cmpLess cmpLess = struct cmpLess*/void outputArray(array<student, arraySize>& inputArray) { cout.setf(cout.left); for (int i = 0; i < arraySize; i++) { cout.width(6); cout << inputArray.at(i).age << ' '; } cout << endl;}void processArray(){ std::srand(time(NULL)); // use current time as seed for random generator array<student, arraySize> myArray; for (int i = 0; i < 10; i++) { student s("abc", rand()); myArray.at(i) = std::move(s); } outputArray(myArray); /* three program *first: define compare function *second: define operator< *third: define compare class */ //sort(myArray.begin(), myArray.end(), [](const student& l, const student& r) {return l.age < r.age;}); //sort(myArray.begin(), myArray.end()); sort(myArray.begin(), myArray.end(), cmpLess()); outputArray(myArray);}struct cmpMap{ bool operator()(const student& l, const student& r) const { return l.age < r.age;}};void outputMap(map<student, int, cmpMap>& inputMap){ for (auto v : inputMap) { cout.width(10); cout.setf(cout.left); cout << v.first.age << ' '; } cout << endl;}void processMap() { std::srand(time(NULL)); // use current time as seed for random generator map<student, int, cmpMap> studentMap; for (int i = 0; i < 10; i++){ student s("abc", rand()); studentMap.insert(map<student, int>::value_type(s, rand())); } outputMap(studentMap);}struct hashKey{ size_t operator()(const student& s) const { return s.age; }};void ouputOrderedMap(unordered_map<student, int, hashKey>& orderedMap){ for (auto tmp : orderedMap) { cout.width(10); cout.setf(cout.left); cout << tmp.first.age << ' '; } cout << endl;}void proceessOrderedMap(){ unordered_map<student, int, hashKey> unorderedMap; for (int i = 0; i < 10; i++) { student s("abd", rand()%100); unorderedMap.insert(make_pair(s, rand())); } ouputOrderedMap(unorderedMap);}int main(){ cout << "=====sort======" << endl; processArray(); cout << "=====map======" << endl; processMap(); cout << "=====unorderted_map======" << endl; proceessOrderedMap(); system("pause"); return 0;}
阅读全文
0 0
- sort/map/unordered_map自定义类型如何构造比较函数
- 如何自定义sort函数中的比较函数
- 【map】【unordered_map】map和unordered_map中键类型为自定义类型的操作
- STL中sort、priority_queue、map、set的自定义比较函数
- STL中sort、priority_queue、map、set的自定义比较函数
- map自定义比较函数
- map自定义比较函数
- map自定义比较函数
- map自定义比较函数
- c++ map hash_map unordered_map 比较
- map hash_map unordered_map 性能比较
- map中自定义比较函数
- map与unordered_map效率的比较
- map,hash_map和unordered_map 实现比较
- map,hash_map和unordered_map 实现比较
- std::sort使用自定义比较函数
- 自定义类型struct的排序-sort函数
- unordered_map & map
- 多个同名ID/class,点击那个取得当前那个的值和属性值
- 23种设计模式之策略模式
- 数学知识小记
- Redis常用命令
- c++作业六
- sort/map/unordered_map自定义类型如何构造比较函数
- CentOS 系统下安装多个tomcat,占用不同的端口
- Map<String, String> 遍历的四种方法
- python 定时执行函数方法
- vue学习笔记(一)(vue webpack+vue-router+nodeJs npm)
- sass基础
- 名企笔试:2016网易笔试题(路灯)2017-05-16 算法爱好者
- Java中利用DatagramPacket与DatagramSocket进行通讯的示例
- 4893: 项链分赃