• 引用内部函数绑定机制,R转义字符,C++引用,别名,模板元,宏,断言,C++多线程,C++智能指针


    

    1、引用内部函数绑定机制

    #include<iostream>

    #include<functional>

     

    usingnamespacestd;

    usingnamespacestd::placeholders;

     

    //仿函数。创建一个函数指针,引用一个结构体内部或者一个类内部的共同拥有函数

    structMyStruct

    {

       voidadd(inta)

       {

           cout <<a <<endl;

       }

       voidadd2(inta,intb)

       {

           cout <<a +b <<endl;

       }

       voidadd3(inta,intb,intc)

       {

           cout <<a +b +c <<endl;

       }

    };

     

    voidmain()

    {

       MyStructstruct1;

       //auto自己主动变量。地址,函数指针,bind绑定

       //第一个參数引用内部函数,绑定一个实体对象

       //这里后面的_1等为占位用

       autofunc =bind(&MyStruct::add, &struct1,_1);

       autofunc2 =bind(&MyStruct::add2, &struct1,_1,_2);

       autofunc3 =bind(&MyStruct::add3, &struct1,_1,_2,_3);

       func(100);

       func2(10, 20);

       func3(10, 20, 30);

     

       cin.get();

    }

     

    voidmain1()

    {

       //假设想通过第二种方式获得结构体中的函数,还能够通过以下的方式

       MyStructstruct1;

       //创建函数指针。类结构体,数据私有,代码共享

       //函数通过调用,调用须要传递对象名进行区分

       void(MyStruct::*p)(inta) = &MyStruct::add;

       

       cin.get();

    }

    补充:Cocos2dx中关于std::function和bind的使用方法案例:

    #include "T01CPP11.h"

     

    void foo()

    {

        CCLog("foo is called ");

    }

     

    void funArg3(int n,char c,float f)

    {

        CCLog("%d,%c,%f",n,c,f);

    }

     

    void T01CPP11::mFoo()

    {

        CCLog("mFoo is called");

    }

     

    //关于lambda表达式

    bool T01CPP11::init()

    {

        Layer::init();

       

        //std::function;

        //std::bind

     

        //函数指针类型

        std::function<void()> func = foo;

        func();

     

        //成员函数指针的赋值和调用

        {

            //注意在::域作用符后面加上*

            void(T01CPP11::*FuncPtr)() = &T01CPP11::mFoo;

            //调用成员函数的时候加上this

            (this->*FuncPtr)();

        }

     

        //bind的功能,就是把一个详细函数,编程std::function对象

        //bind能够把详细函数和std::function形式全然改变,比方參数数量的改变

        {

            std::function<void()> func = std::bind(funArg3, 100, 'c', 0.1f);

            func();

        }

     

        //也能够改变參数顺序

        {

            //当中

            //_1:表示function<void(float, char, int)>括号里的第一个參数

            //_2:表示function<void(float, char, int)>括号里的第二个參数

            //_3:表示function<void(float, char, int)>括号里的第三个參数

            //后面3个占位符分别在funArg3中的顺序,而又用标记来代表上面括号里參数的的位置

            std::function<void(float, char, int)> func = std::bind(funArg3,

                std::placeholders::_3, std::placeholders::_2, std::placeholders::_1);

            func(1.0f, 'd', 2000);

        }

     

        // 也能够同一时候改变參数个数和顺序

        {

            std::function<void(float, char)> func = std::bind(funArg3,

                100, std::placeholders::_2, std::placeholders::_1);

            func(4.0f, 'x');

        }

     

        //也能够绑定成员函数

        {

            std::function<void()> func = std::bind(&T01CPP11::mFoo, this);

            func();

        }

     

        return true;

    }


    2.通过R”()”的方式实现转义字符

    #include<iostream>

    #include<string>

    #include<stdlib.h>

     

    voidmain()

    {

       std::stringpath =R"( "C:Program FilesTencentQQQQProtectBinQQProtect.exe")";

       //通过R"()" 括号之间去掉转义字符

       system(path.c_str());

       system("pause");

    }

    3.引用

    #include<iostream>

     

    template<classT>

    voidcom(Targ) //模板函数,引用无效,引用包装器

    {

       std::cout << "com =" << &arg << " ";

       arg++;

    }

     

    voidmain()

    {

       intcount = 10;

       int &rcount =count;

       com(count);

       std::cout << count <<std::endl;

       //std::ref(变量),函数模板,引用包装器

       //com(std::ref(count));

       com(rcount);

       std::cout << "main=" << &rcount << " ";

       std::cout << count <<std::endl;

       std::cin.get();

    }

    4.C++别名

    #include<iostream>

     

    namespacespace{ //隔离模板,避免冲突

       template<classT>usingprt =T*;//模板的简写,定义一个模板的指针

    }

     

    intadd(inta,intb)

    {

       returna +b;

    }

     

    //typedefC语言中定义别名的keyword

    typedef int(*ADD)(inta,intb);

    //C++中的别名是通过usingkeyword实现的

    usingFUNC =int(*)(inta,intb);

    usingco =std::ios_base::fmtflags;

     

    voidmain()

    {

       ADDp =add;

       std::cout << p(1, 2) <<std::endl;

       FUNCfunc =add;

       std::cout << func(1, 2) <<std::endl;

       //space::ptr<int> pint(new int(15));

       //std::cout << *pint << "  " << pint << std::endl;

       std::cin.get();

    }

    5.模板元

    #include<iostream>

     

    //主要思想

    //

    //利用模板特化机制实现编译期条件选择结构,利用递归模板实现编译期循环结构,模板元程序则由编译器在编译期解释运行。

    //

    //优劣及适用情况

    //

    //通过将计算从执行期转移至编译期,在结果程序启动之前做尽可能多的工作,终于获得速度更快的程序。也就是说模板元编程的优势在于:

    //

    //1.以编译耗时为代价换来卓越的执行期性能(一般用于为性能要求严格的数值计算换取更高的性能)。

    通常来说。一个有意义的程序的执行次数(或服役时间)总是远远超过编译次数(或编译时间)。

    //

    //2.提供编译期类型计算。通常这才是模板元编程大放异彩的地方。

    //

    //模板元编程技术并不是都是长处:

    //

    //1.代码可读性差。以类模板的方式描写叙述算法或许有点抽象。

    //

    //2.调试困难,元程序运行于编译期,没实用于单步跟踪元程序运行的调试器(用于设置断点、察看数据等)。程序猿可做的仅仅能是等待编译过程失败。然后人工破译编译器倾泻到屏幕上的错误信息。

    //

    //3.编译时间长。通常带有模板元程序的程序生成的代码尺寸要比普通程序的大。

    //

    //4.可移植性较差,对于模板元编程使用的高级模板特性。不同的编译器的支持度不同。

     

    //模板元吧执行时消耗的时间,在编译期间优化

    template<intN>

    structdata

    {

       enum {res =data<N - 1>::res +data<N - 2>::res };

    };

     

    //当为1的情况

    template<>

    structdata<1>

    {

       enum {res = 1};

    };

     

    template<>

    structdata<2>

    {

       enum {res = 1 };

    };

     

    intgetdata(intn)

    {

       if (n == 1 ||n == 2)

       {

           return 1;

       }

       else

       {

           returngetdata(n - 1) + getdata(n - 2);

       }

    }

     

    voidmain()

    {

       constintmyint = 40;

       intnum =data<myint>::res;//<>内部不能够有变量

       std::cout << num <<std::endl;

       std::cout << getdata(40) <<std::endl;

     

       std::cin.get();

    }

    执行结果同样。可是后者明显速度要慢于前者。

    6.

    #include<stdio.h>

    #include<assert.h>

    #include<iostream>

     

    usingnamespacestd;

    #define N 10

    voidmain()

    {

       intnum = 100;

       cout <<num <<endl;

       //本文件所在的文件路径

       cout <<__FILE__ <<endl;

       //下一行代码在文件里的行位置

       cout <<__LINE__ <<endl;

       //日期

       cout <<__DATE__ <<endl;

       //日期

       cout <<__TIME__ <<endl;

       //当前函数名称

       cout << __FUNCTION__ <<endl;

     

       cin.get();

    }

    7.断言调试

    这时候没有输入东西

    8.C++中的多线程

    #include<thread>

    #include<iostream>

    #include<windows.h>

    #include<vector>

     

    usingnamespacestd;

    usingnamespacestd::this_thread;

     

    voidmsg()

    {

       MessageBoxA(0,"12345","678910",0);

    }

     

    voidmsgA(intnum)

    {

       std::cout << get_id() <<" num = " <<num <<std::endl;

    }

     

    voidmain()

    {

       // thread::hardware_concurrency线程

       auton =thread::hardware_concurrency();//获得当前核心数

       std::cout << n <<std::endl;

       //获取当前线程编号

       std::cout << "thread = " <<get_id() <<std::endl;

     

       threadthread1(msg);//创建多线程

       threadthread2(msg);

       thread1.join();//開始运行

       thread2.join();

     

       std::cin.get();

    }

    截图例如以下:

    9.多线程

    #include<thread>

    #include<iostream>

    #include<windows.h>

    #include<vector>

    usingnamespacestd;

    usingnamespacestd::this_thread;

    voidmsg()

    {

       MessageBoxA(0,"12345","678910",0);

    }

    voidmsgA(intnum)

    {

       std::cout << get_id() <<" num = " <<num <<std::endl;

    }

    voidmain()

    {

       vector<thread *> threads;

       for (inti = 0;i < 10;i++)

       {

           threads.push_back(newthread(msg));//创建线程

       }

       for (autoth :threads)

       {

           th->join();

       }

       std::cin.get();

    }

    10.线程间通信

    #include<thread>

    #include<iostream>

    #include<windows.h>

    #include<vector>

     

    usingnamespacestd;

    usingnamespacestd::this_thread;

     

    voidmsgA(intnum)

    {

       std::cout << get_id() <<" num = " <<num <<std::endl;

    }

     

    voidmain()

    {

       vector<thread *> threads;

       for (inti = 0;i < 10;i++)

       {

           //当中后面的msgA为函数名。i为为函数传递的參数

           threads.push_back(newthread(msgA,i));//创建线程

       }

     

       for (autoth :threads)

       {

           th->join();

       }

       std::cin.get();

    }

    程序执行结果例如以下:

    11.C++中的智能指针

    #include<iostream>

    #include<memory>//内存

     

    voidmain()

    {

       for (inti = 0;i < 10000000;i++)

       {

           //新型指针,新型的数组

           std::unique_ptr<double>pdb(newdouble);

           //通过指针运行来自己主动释放内存

     

           //double *p = new double;

       }

     

       std::cin.get();

    }

    12.第二种智能指针

    #include<iostream>

    voidmain()

    {

       //auto_prt

       for (inti = 0;i < 10000000;i++)

       {

           double *p =newdouble;//为指针分配内存

           std::auto_ptr<double>autop(p);

           //创建智能指针管理指针p指向内存

           //智能指针

           //delete p;

       }

       std::cin.get();

    }

  • 相关阅读:
    C#面向对象编程
    WPF Storyboard的启动
    WPF中的窗体Show()和ShowDialog()区别。
    四元数
    小学生四则运算
    小学生四则运算
    javascript ===与==的区别
    a标签的href与onclick中使用js的区别
    10步让你成为更优秀的程序员
    检查SQL Server被哪个进程占用,且杀进程。
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7082488.html
Copyright © 2020-2023  润新知