STL——map 排序相关

声明变量时指定排序规则 通过指定模板的第三个参数,对象类型,C++ 2a 支持 lambda 对象 struct MyCom{ bool operator()(const string key1, const string key2)const{ return key1 > key2; } }; int main() { // lambda 表达式对象 auto cmp = [](const auto& key1, const auto& key2){return key1 < key2;}; map<string, int, decltype(cmp)> myMap1 = {{"RAM", 20}, {"GPU", 15}, {"CPU", 10} }; // 函数对象 map<string, int, MyCom> myMap2 = {{"CPU", 10}, {"GPU", 15}, {"RAM", 20}}; for(const auto& item : myMap1){ cout << item....

August 30, 2022 · 1 min · Rick Cui

函数对象适配器

仿函数适配器 bind1st、bind2nd 将二元仿函数转为一元仿函数 仿函数适配器 not1、not2 仿函数适配器 ptr_fun 将普通函数转为函数对象,然后就可以与其它仿函数适配器一起使用了 仿函数适配器 mem_fun、mem_fun_ref 将成员函数转为适配器 class MyPrint : public binary_function<int, int, void>{ public: void operator()(int v, int val) const{ cout << "v: " << v << ", val: " << val << ", v + val: " << v + val << endl; } }; void myPrint(int v, int val){ cout << v + val << " "; } class MySort: public binary_function<int, int, bool>{ public: bool operator() (int lhs, int rhs)const{ return lhs > rhs; } }; class MyGreater: public unary_function<int, bool>{ public: bool operator()(int v)const{ return v > 50; } }; void printVec(const vector<int> &v){ for(const auto &p : v){ cout << p << " "; } cout << endl; } class Person{ public: Person(int id, int age):id(id), age(age){} void show(){ cout << "id: " << id << ", age: " << age << endl; } int id; int age; }; int main(){ vector<int> v; for(int i = 0; i < 10; ++i){ v....

January 27, 2022 · 2 min · Rick Cui

机房预约系统案例

January 24, 2022 · 0 min · Rick Cui

演讲比赛流程管理案例

January 22, 2022 · 0 min · Rick Cui

常用算法

一、常用遍历算法 1. for_each 2. transform 搬运的目标容器必须要提前开辟空间(resize 而不是 reserve),否则无法正常搬运 int main() { vector<int> v; for(int i = 0; i < 10; ++i){ v.push_back(i); } vector<int> v1; v1.resize(v.size()); // 要用 resize, 不能用 reserve transform(v.begin(), v.end(), v1.begin(), [](int v){ return v * 2;}); for_each(v1.begin(), v1.end(), [](int v){ cout << v << " "; }); cout << endl; return 0; } 二、常用查找算法 1. find 2. find_if 3. adjacent_find 4. binary_search 无序序列不可用 5....

January 22, 2022 · 2 min · Rick Cui