C++笔记——20231130

    

    // 引子

    // if else实现比较赋值
    int a = 0, b = 9, c = 2;
    if (c == 0) {
        a = -1;
    }
    else {
        a = b / c;
    }

    // 三元运算符实现比较赋值
    int e = 13, f = 5;
    int d = f == 0 ? -1 : e / f; // 更简洁,减少代码行数,降低圈复杂度

    // lambda表达式有类似效果

//lambda表达式简介

// [capture-list] (parameters) mutable -> return-type { statement }
    // 捕捉列表  参数  返回值  函数体
    // // 一般是局部匿名函数  也可以写到全局
    // // [] {}不可省略,其它都可省略
    /*    mutable: 默认情况下,lambda函数总是一个const函数,mutable可以取消其常量性。
        使用该修饰符时,参数列表不可省略(即使参数为空时,也要带上小括号)。
        mutable 只是让传值捕捉变量const属性去掉了,但是捕捉的a,b仍是拷贝,外部的a,b无法被修改,所以mutable 很少用,意义不大
    */
    // 例子:[](int x, int y)->int{return x + y;};
    

return-type用法

    /*
    return-type省略时,自动推到返回值类型 */
    int a = 0, b = 200;
    auto add1 = [](int x, int y)->double {return (x + y) / 3.0; };
    auto add2 = [](int x, int y) {return (x + y) / 3.0; }; // 自动推导返回值类型
    auto add3 = [](int x, int y)->int {return (x + y) / 3.0; }; // 强转返回值类型
    cout << "return-type用法举例 :" << add1(a, b) << endl;
    cout << "自动推到返回值类型  :" << add2(a, b) << endl;
    cout << "强转返回值类型      :" << add3(a, b) << endl;
    cout << endl;


    /*

    [] :捕捉变量 */

    auto add4 = [](int x, int y) {return (x + y) / 3.0; }; //传参写法
    auto add5 = [a, b] {return (a + b) / 3.0; }; //捕捉变量写法
    cout << "传参写法     :" << add4(a, b) << endl;
    cout << "捕捉变量写法 :" << add5() << endl;
    cout << endl;

    /*

    swap实现方式 :[] 或 () */

    // () 参数列表 值传递实现不了 申请一块地址存放参数的值
    a = 10, b = 20;
    cout << "swap1 : a = " << a << "; b = " << b << endl;
    auto swap1 = [](int x, int y) {
        int tmp = x;
        x = y;
        y = tmp;
    };
    swap1(a, b);
    cout << "swap1 : a = " << a << "; b = " << b << endl << endl;
    //cout << "---------------------------";

    // () 参数列表 引用传递 可以实现(值传递地址) 申请一块地址存放参数的地址
    a = 10, b = 20;
    cout << "swap2 : a = " << a << "; b = " << b << endl;
    auto swap2 = [](int& x, int& y) {
        int tmp = x;
        x = y;
        y = tmp;
    };
    swap2(a, b);
    cout << "swap2 : a = " << a << "; b = " << b << endl << endl;

    // [] 捕捉列表 用值的方式捕捉 不行
    a = 10, b = 20;
    cout << "swap3 : a = " << a << "; b = " << b << endl;
    auto swap3 = [a, b]()mutable { // mutable 去const属性,否则 a b不允许赋值,意义不大,赋值后外部也用不了
        int tmp = a;
        a = b;
        b = tmp;
    };
    swap3();
    cout << "swap3 : a = " << a << "; b = " << b << endl << endl;


    // [] 捕捉列表 用引用的方式捕捉 可以
    a = 10, b = 20;
    cout << "swap4 : a = " << a << "; b = " << b << endl;
    auto swap4 = [&a, &b]{
        int tmp = a;
        a = b;
        b = tmp;
    };
    swap4();
    cout << "swap4 : a = " << a << "; b = " << b << endl << endl;


    /*

    [] 捕捉用法 */

    int c = 2, d = 3, e = 4, f = 5, g = 6, ret;
    // 传值捕捉全部对象 [=]{}
    auto func1 = [=] {
        return c + d * e / f + g;
    };

    cout << "传值捕捉全部对象 :  " << func1() << endl;

    // 传引用捕捉全部对象 [&]{}
    auto func2 = [&] {
        ret = c + d * e / f + g;
    };
    func2();
    cout << "传引用捕捉全部对象 :" << ret << endl;

    // 混合捕捉
    auto func3 = [c, d, &ret] {
        ret = c + d;
    };
    func3();
    cout << "混合捕捉1 :    " << ret << endl;

    // ret传引用捕捉 其他全部传值捕捉
    auto func4 = [=, &ret] {
        ret = c + d * e / f + g;
        //c = 1;
    };
    func4();
    cout << "混合捕捉2 :   " << ret << endl << endl;

/*
应用:替代仿函数 */

// 排序,按分数从高到底排序,分数相等时按姓名从小到大排序

// 仿函数实现

#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

struct node {
    string name;
    int score;
};

node a[5] = {
    {"aaa", 90},
    {"aa", 98},
    {"bbb", 100},
    {"abc", 98},
    {"aaaa", 90}
};

static bool myComp(const node& x, const node& y) {
    if (x.score != y.score) {
        return x.score > y.score;
    }
    return x.name < y.name;
}

int main() {
    for (int i = 0; i < 5; ++i) {
        cout <<  "排序前 姓名:" << setw(5) << a[i].name  << "  分数 = " << setw(4) << a[i].score << endl;
    }
    cout << endl;

    sort(a, a + 5, myComp);

    for (int i = 0; i < 5; ++i) {
        cout << "排序后 姓名:" << setw(5) << a[i].name << "  分数 = " << setw(4) << a[i].score << endl;
    }
}

// lambda实现

#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;

struct node {
    string name;
    int score;
};

node a[5] = {
    {"aaa", 90},
    {"aa", 98},
    {"bbb", 100},
    {"abc", 98},
    {"aaaa", 90}
};

int main() {
    for (int i = 0; i < 5; ++i) {
        cout <<  "排序前 姓名:" << setw(5) << a[i].name  << "  分数 = " << setw(4) << a[i].score << endl;
    }
    cout << endl;

    sort(a, a + 5, [](node x, node y) {
        if (x.score != y.score) {
            return x.score > y.score;
        }
        return x.name < y.name;
    });

    for (int i = 0; i < 5; ++i) {
        cout << "排序后 姓名:" << setw(5) << a[i].name << "  分数 = " << setw(4) << a[i].score << endl;
    }
}

// 应用2

// 以下代码运行结果

#include <iostream>

int main() {
    int number = 1;

    auto func1 = [=]() { return number; };
    auto func2 = [&]() { return number; };

    number++;

    std::cout << func1() << " " << func2();

    return 0;
}
//输出结果是:1 2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值