天天看點

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



1、引用内部函數綁定機制

#include<iostream>

#include<functional>

usingnamespacestd;

usingnamespacestd::placeholders;

//仿函數,建立一個函數指針,引用一個結構體内部或者一個類内部的共有函數

structmystruct

{

   voidadd(inta)

   {

       cout

<<a <<endl;

   }

   voidadd2(inta,intb)

<<a +b

<<endl;

   voidadd3(inta,intb,intc)

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

   //如果想通過另外一種方式獲得結構體中的函數,還可以通過下面的方式

   //建立函數指針,類結構體,資料私有,代碼共享

   //函數通過調用,調用需要傳遞對象名進行區分

   void(mystruct::*p)(inta)

= &mystruct::add;

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

補充:cocos2dx中關于std::function和bind的用法案例:

#include

"t01cpp11.h"

void

foo()

    cclog("foo is called\n");

funarg3(int n,char

c,float

f)

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

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

    // 也可以同時改變參數個數和順序

char)> func =

std::bind(funarg3,

            100, std::placeholders::_2,

        func(4.0f,

'x');

    //也可以綁定成員函數

func = std::bind(&t01cpp11::mfoo,

this);

    return

true;

2.通過r”()”的方式實作轉義字元

#include<string>

#include<stdlib.h>

   std::stringpath

=r"( "c:\program files\tencent\qq\qqprotect\bin\qqprotect.exe")";

   //通過r"()" 括号之間去掉轉義字元

   system(path.c_str());

   system("pause");

3.引用

template<classt>

voidcom(targ) //模闆函數,引用無效,引用包裝器

   std::cout

<< "com =" << &arg

<< "\n";

   arg++;

   intcount

= 10;

   int &rcount

=count;

   com(count);

<< count <<std::endl;

   //std::ref(變量),函數模闆,引用包裝器

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

   com(rcount);

<< "main=" << &rcount

   std::cin.get();

4.c++别名

namespacespace{ //隔離模闆,避免沖突

   template<classt>usingprt

=t*;//模闆的簡寫,定義一個模闆的指針

intadd(inta,intb)

   returna

+b;

//typedef是c語言中定義别名的關鍵字

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

//c++中的别名是通過using關鍵字實作的

usingfunc

=int(*)(inta,intb);

usingco

=std::ios_base::fmtflags;

   addp

=add;

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

   funcfunc

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

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

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

<< pint << std::endl;

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

5.模闆元

//主要思想

//

//利用模闆特化機制實作編譯期條件選擇結構,利用遞歸模闆實作編譯期循環結構,模闆元程式則由編譯器在編譯期解釋執行。

//優劣及适用情況

//通過将計算從運作期轉移至編譯期,在結果程式啟動之前做盡可能多的工作,最終獲得速度更快的程式。也就是說模闆元程式設計的優勢在于:

//1.以編譯耗時為代價換來卓越的運作期性能(一般用于為性能要求嚴格的數值計算換取更高的性能)。通常來說,一個有意義的程式的運作次數(或服役時間)總是遠遠超過編譯次數(或編譯時間)。

//2.提供編譯期類型計算,通常這才是模闆元程式設計大放異彩的地方。

//模闆元程式設計技術并非都是優點:

//1.代碼可讀性差,以類模闆的方式描述算法也許有點抽象。

//2.調試困難,元程式執行于編譯期,沒有用于單步跟蹤元程式執行的調試器(用于設定斷點、察看資料等)。程式員可做的隻能是等待編譯過程失敗,然後人工破譯編譯器傾瀉到螢幕上的錯誤資訊。

//3.編譯時間長,通常帶有模闆元程式的程式生成的代碼尺寸要比普通程式的大,

//4.可移植性較差,對于模闆元程式設計使用的進階模闆特性,不同的編譯器的支援度不同。

//模闆元吧運作時消耗的時間,在編譯期間優化

template<intn>

structdata

   enum

{res =data<n

- 1>::res +data<n

- 2>::res };

//當為1的情況

template<>

structdata<1>

{res = 1};

structdata<2>

{res = 1 };

intgetdata(intn)

   if

(n == 1 ||n

== 2)

       return

1;

   else

       returngetdata(n

- 1) + getdata(n

- 2);

   constintmyint

= 40;

   intnum

=data<myint>::res;//<>内部不可以有變量

<< num <<std::endl;

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

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

運作結果相同,但是後者明顯速度要慢于前者。

6.宏

#include<stdio.h>

#include<assert.h>

#define n

10

= 100;

   cout

<<num <<endl;

   //本檔案所在的檔案路徑

<<__file__ <<endl;

   //下一行代碼在檔案中的行位置

<<__line__ <<endl;

   //日期

<<__date__ <<endl;

<<__time__ <<endl;

   //目前函數名稱

<< __function__ <<endl;

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

7.斷言調試

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

這時候沒有輸入東西

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

8.c++中的多線程

#include<thread>

#include<windows.h>

#include<vector>

usingnamespacestd::this_thread;

voidmsg()

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

voidmsga(intnum)

<< get_id() <<"

num = " <<num

<<std::endl;

   // thread::hardware_concurrency線程

   auton

=thread::hardware_concurrency();//獲得目前核心數

<< n <<std::endl;

   //擷取目前線程編号

<< "thread = " <<get_id()

   threadthread1(msg);//建立多線程

   threadthread2(msg);

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

   thread2.join();

截圖如下:

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

9.多線程

   vector<thread

*> threads;

   for

(inti

= 0;i < 10;i++)

       threads.push_back(newthread(msg));//建立線程

(autoth

:threads)

       th->join();

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

10.線程間通信

       //其中後面的msga為函數名,i為為函數傳遞的參數

       threads.push_back(newthread(msga,i));//建立線程

程式運作結果如下:

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

11.c++中的智能指針

#include<memory>//記憶體

= 0;i < 10000000;i++)

       //新型指針,新型的數組

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

       //通過指針執行來自動釋放記憶體

       //double *p = new double;

12.另外一種智能指針

   //auto_prt

       double

*p =newdouble;//為指針配置設定記憶體

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

       //建立智能指針管理指針p指向記憶體

       //智能指針

       //delete p;

繼續閱讀