首页 > 编程语言 >C++11

C++11

时间:2022-09-26 21:55:23浏览次数:72  
标签:11 function return int long person C++ 引用

1.新增不需要赋值即可初始化

    //C++98
    int a = 1;
    int* p1 = new int[3];
    Date d1(2022, 9, 26);

    //C++11
    Date d2{ 2022,9,27 };
    int* p2 = new int[3]{ 1,2,3 };
    int b{ 1 };
    Date* p4 = new Date[3]{ {2022,9,28},{2022,9,29},{2022,9,30} };

2.右值引用

1.左值引用 只能引用左值,不能引用右值

const 左值 可以引用左值,也可以引用右值

    //左值引用
    int c = 1;
    int& rc = c;
    //int& rv = 20;  ×  左值不允许引用右值
    const int& rv = 20;//因为20是常量  关乎到权限的问题 所以可以引用
    const int& rb = c;
2.右值引用
    //右值引用
    int&& r1 = 10;
    //int&& r2 = c; 右值引用不允许引用左值
    int&& rb = move(c);//move能把左值该为右值  但是他的数据会被转移 所以谨慎使用
    string s1("111");
    string s2 = (move(s1));
    //s1 的111被move转移到了s2  所以现在s1为空
两者的差别在与 坐值引用可以取地址  右值引用不可以取地址

3.可变模版参数

// Args是一个模板参数包,args是一个函数形参参数包
// 声明一个参数包Args...args,这个参数包中可以包含0到任意个模板参数。
template <class ...Args>
void ShowList(Args... args)
{}

4.lambda表达式

    //lambda表达式
    int d = 1;
    int e = 2;

    auto add1 = [](int a, int b)->int {return a + b; };
    add1(1, 2);

    auto add2=[](int a, int b){return a + b; };
    add2(1, 2);

    auto add3 = [=] {return d + e; };//全部传值
    //auto add3=[=]{return d=e;};
    //传值 不允许被修改
    add3();
    auto add4 = [&] {return d = e + 2; };//全部传引用
    add4();
    auto add5 = [&, d] {return e = d + 2; };//d用传值 其他全部传引用
    add5();
    auto add6 = [=, &d] {return d = e + 2; };//d用传引用 其他全部传值
    add6();

5.包装器

    //包装器
    function<int(int, int)> fun1 = f;
    fun1(1, 2);
    function<int(int, int)> fun2 = person::a;//调用静态成员函数
    fun2(1, 2);
    function<int(person,int, int)> fun3 = &person::b;//调用普通成员函数
    fun3(person(), 1, 2);

150. 逆波兰表达式求值 - 力扣(LeetCode)

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
    stack<long long> s;
    map<string,function<long long(long long,long long)>> func=
    {
        {"+",[](long long a,long long b){return a+b;}},
        {"-",[](long long a,long long b){return a-b;}},
        {"*",[](long long a,long long b){return a*b;}},
        {"/",[](long long a,long long b){return a/b;}}
    };
    
    for(auto& e:tokens)
    {
        if(func.count(e))//判断e是否存在map中  存在就是操作符
        {
            long long right=s.top();
            s.pop();
            long long left=s.top();
            s.pop();

            s.push(func[e](left,right));
        }
        else//不存在就是操作数
        {
            s.push(stoll(e));
        }
    }
    return s.top();
    }
};

6.bind

    //bind
    function<int(int, int)> fun1 = bind(f,placeholders::_2,placeholders::_1);
    fun1(1, 2);//bind调整顺序
    function<int(int, int)> fun2 = person::a;//调用静态成员函数
    fun2(1, 2);
    function<int(person, int, int)> fun3 = &person::b;//调用普通成员函数  //不用bind时 function参数要加person
    fun3(person(), 1, 2);
    function<int(int, int)> fun4 = std::bind(&person::b,person(),placeholders::_1, placeholders::_2); //调用普通成员函数   用bind时 function参数不用加person
    fun4(1, 2);//bind调整参数个数

7.整体代码

#include<iostream>
#include<functional>
using namespace std;

class Date
{
public:
    Date(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;
    }

private:
    int year;
    int month;
    int day;
};

template<class T>
void a(T&& t)//传左值变左值 传右值变左值
{
    func(t);//这时候的t已经是左值了  
    //因为被模版折叠
    //但可以move改变为右值
    func(move(t));//但t的资源会被转移
    func(forward<T>(t));//完美转发 转为右值 并且t的资源不会被转移
}

class person
{
    //person() = default;
    //强制生存默认构造或者其他的拷贝赋值等...

    //person() = delete;
    //不让调用并且不让生存默认构造或者其他的拷贝赋值等...

public:

    static int a(int a, int b)
    {
        return a + b;
    }

    int b(int a, int b)
    {
        return a + b;
    }

};


// Args是一个模板参数包,args是一个函数形参参数包
// 声明一个参数包Args...args,这个参数包中可以包含0到任意个模板参数。
template <class ...Args>
void ShowList(Args... args)
{}

int f(int a,int b)
{
    return a + b;
}




int main()
{
    //C++98
    int a = 1;
    int* p1 = new int[3];
    Date d1(2022, 9, 26);

    //C++11
    Date d2{ 2022,9,27 };
    int* p2 = new int[3]{ 1,2,3 };
    int b{ 1 };
    Date* p4 = new Date[3]{ {2022,9,28},{2022,9,29},{2022,9,30} };

    //左值引用
    int c = 1;
    int& rc = c;
    //int& rv = 20;  ×  左值不允许引用右值
    const int& rv = 20;//因为20是常量  关乎到权限的问题 所以可以引用
    const int& rb = c;

    //右值引用
    int&& r1 = 10;
    //int&& r2 = c; 右值引用不允许引用左值
    int&& rb = move(c);//move能把左值该为右值  但是他的数据会被转移 所以谨慎使用

    string s1("111");
    string s2 = (move(s1));
    //s1 的111被move转移到了s2  所以现在s1为空



    //lambda表达式
    int d = 1;
    int e = 2;

    auto add1 = [](int a, int b)->int {return a + b; };
    add1(1, 2);

    auto add2=[](int a, int b){return a + b; };
    add2(1, 2);

    auto add3 = [=] {return d + e; };//全部传值
    //auto add3=[=]{return d=e;};
    //传值 不允许被修改
    add3();
    auto add4 = [&] {return d = e + 2; };//全部传引用
    add4();
    auto add5 = [&, d] {return e = d + 2; };//d用传值 其他全部传引用
    add5();
    auto add6 = [=, &d] {return d = e + 2; };//d用传引用 其他全部传值
    add6();

    //包装器
    function<int(int, int)> fun1 = f;
    fun1(1, 2);
    function<int(int, int)> fun2 = person::a;//调用静态成员函数
    fun2(1, 2);
    function<int(person,int, int)> fun3 = &person::b;//调用普通成员函数
    fun3(person(), 1, 2);

    //bind
    function<int(int, int)> fun1 = bind(f,placeholders::_2,placeholders::_1);
    fun1(1, 2);//bind调整顺序
    function<int(int, int)> fun2 = person::a;//调用静态成员函数
    fun2(1, 2);
    function<int(person, int, int)> fun3 = &person::b;//调用普通成员函数  //不用bind时 function参数要加person
    fun3(person(), 1, 2);
    function<int(int, int)> fun4 = std::bind(&person::b,person(),placeholders::_1, placeholders::_2); //调用普通成员函数   用bind时 function参数不用加person
    fun4(1, 2);//bind调整参数个数

    return 0;
}

标签:11,function,return,int,long,person,C++,引用
From: https://www.cnblogs.com/LonelyMoNan/p/16732360.html

相关文章

  • CSP-S模拟11
    A.回文经典\(dp\),两边同时走,三维状态表示走了几步,左上出发走到哪行,右下出发走到哪行code#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;......
  • 36th 2022/8/11 模拟赛总结25
    这次真的不好,比赛一开始,揪着T1不放,当时脑子还短路,没想到讲循环一个个压回去,就是坐在那不停地想:斐波那契数列该乘什么……根本就像在发呆一样,足足发了一个钟,然后突然想到,可......
  • CF1155D 题解
    题目传送门题目分析说实话,第一眼这题以为是贪心...(事实上我看所有动态规划都像贪心)。然后接着发现贪心明显做不了,又看见最大子段和,很容易联想到\(\text{dp}\)。在设计......
  • C++多线程编程之【线程管理】
    1.如何启动线程?构建std::thread对象即可。直接传函数名(地址)创建一个类并创建伪函数。构建对象(实例化),将对象作为参数传入thread对象实例化。2.为什么要等待线程?首先......
  • C/C++ __cdecl和__stdcall的区别和联系
    函数的调用约定涉及了函数参数的入栈顺序、清栈主体(负责清理栈的主体:函数自身还是调用函数者?)、部分名称重整。如,在C编译方式下有_stdcall、_cdecl等调用约定,在C++编译方式......
  • C/C++ x86-64的调用约定,忽略__stdcall、__cdecl、__fastcall、_thiscal
    在设计调用约定时,x64体系结构利用机会清除了现有Win32调用约定(如__stdcall、__cdecl、__fastcall、_thiscall等)的混乱。在Win64中,只有一个本机调用约定而__cdecl......
  • Win10使用打印机0x0000011b错误 如何处理(没有KB5005565补丁如何解决??)
    1.排查问题win10连接打印机共享错误显示0x0000011b怎么解决?很多用户在更新了windows系统的最新补丁后,突然发现自己打开打印机的时候提示“无法连接到打印机,错误为0x000......
  • C++ 导入动态链接库DLL 中的函数
    C++导入动态链接库DLL中的函数声明头文件<windows.h>,利用windows库进行DLL的加载#include<windows.h>然后用typedef定义一个指针函数类型typedefvoid(**fun),这......
  • C++自学笔记
    初始化pA(){p=0;cout<<"A::A()"<<endl;}初始化列表InitializerlistA():p(0){cout<<"A::A()"<<endl;}      初始化vs赋值   赋值=默认初始化+......
  • C++ 数组指针累加后的偏移量
    typedefstruct_MyStruct{ inta; intb; intc; _MyStruct(){a=0;b=0;c=0;}}MyStruct;intmain(){#if1 MyStruct*st[3]; //数组指针 MyStruct......