引用内部函数绑定机制,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\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;
}
?
//typedef是C語言中定義別名的關鍵字
typedef?int(*ADD)(inta,intb);
//C++中的別名是通過using關鍵字實現的
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();
}
總結
以上是生活随笔為你收集整理的引用内部函数绑定机制,R转义字符,C++引用,别名,模板元,宏,断言,C++多线程,C++智能指针的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联想装系统bios怎么设置u盘启动 设置
- 下一篇: C++中的数组array和vector,