引用內部函數綁定機制,R轉義字符,C++引用,別名,模板元,宏,斷言,C++多線程,C++智能指針



1、引用內部函數綁定機制ios

#include<iostream>算法

#include<functional>編程

 

usingnamespacestd;windows

usingnamespacestd::placeholders;數組

 

//仿函數。建立一個函數指針,引用一個結構體內部或者一個類內部的共同擁有函數多線程

structMyStruct函數

{post

   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\n");

}

 

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 Files\Tencent\QQ\QQProtect\Bin\QQProtect.exe")";

   //經過R"()" 括號之間去掉轉義字符

   system(path.c_str());

   system("pause");

}

3.引用

#include<iostream>

 

template<classT>

voidcom(Targ) //模板函數,引用無效,引用包裝器

{

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

   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 << "\n";

   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();

}

相關文章
相關標籤/搜索