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

转义字符  时间:2021-04-20  阅读:()

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

#include<iostream>

#include<functional>u s ingnamespac est d;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 = b ind(&MyStruct: :add2, &struct1, _1, _2) ;autofunc3 = b ind(&MyStruct: :add3, &struct1, _1, _2, _3) ;f unc(100) ;f unc 2(10, 20) ;f unc 3(10, 20, 30) ;cin. get() ;

}voidmain1()

{

//如果想通过另外一种方式获得结构体中的函数还可以通过下面的方式

MyStructstruct1;

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

//函数通过调用调用需要传递对象名进行区分void(MyStruct: :*p) (inta) =&MyStruct: :add;cin. get() ;

}

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

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.引用包装器通过std: :ref(count) 的方式调用。

#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)) ;c om(rcount) ;std: :cout<<"main="<<&rcount<<"\n";std: :cout<<count<<std: :endl;std: :cin.get() ;

}

使用引用包装器的方式

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

FUNC func = 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 + dat a<N - 2>: :re s } ;} ;

//当为1的情况template<>structdata<1>

{e num {r e s = 1} ;

} ;template<>structdata<2>

{e num { r e s = 1 } ;

} ;intgetdata(intn)

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

{return 1;

}else

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

}

}voidmain()

{constintmyint = 40;i ntnum = data<my int>: :res;//<>内部不可以有变量std: :cout<<num<<std: :endl ;std: :cout<<getdata(40) <<std: :endl ;std: :cin.get() ;

}

运行结果相同但是后者明显速度要慢于前者。

6.宏

#include<stdio.h>

#include<assert.h>

#include<iostream>u s ingnamespac est d;

#def ineN 10voidmain()

{i ntnum = 100;c out<<num<<endl;

//本文件所在的文件路径cout<<__FILE__<<endl;

//下一行代码在文件中的行位置cout<<__LINE__<<endl;

//日期c out<<__DATE__<<end l;

//日期cout<<__TIME__<<endl;

//当前函数名称cout<< __FUNCTION__ <<endl;cin. get() ;

}

7.断言调试注意static_assert这个是C++11的语法在QT中运行需要加上CONFIG+=C++11

这时候没有输入东西

8.C++中的多线程

#inc lude<thread>

#include<iostream>

#inc lude<windows.h>

#include<vector>u s ingnamespac est d;usingnamespacestd: :thi s_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() ;//获得当前cpu核心数我的是4核的std: :cout<<n<<std: :endl;

//获取当前线程编号std: :cout<<"thread = "<<get_id() <<std: :endl;threadthread1 (ms g) ; //创建多线程threadthread2(ms g) ;thread1. join() ;//开始执行

Digital-VM:服务器,$80/月;挪威/丹麦英国/Digital-VM:日本/新加坡/digital-vm:日本VPS仅$2.4/月

digital-vm怎么样?digital-vm在今年1月份就新增了日本、新加坡独立服务器业务,但是不知为何,期间终止了销售日本服务器和新加坡服务器,今天无意中在webhostingtalk论坛看到Digital-VM在发日本和新加坡独立服务器销售信息。服务器硬件是 Supermicro、采用最新一代 Intel CPU、DDR4 RAM 和 Enterprise Samsung SSD内存,默认...

美国服务器20G防御 50G防御 688元CN2回国

全球领先的IDC服务商华纳云“美国服务器”正式发售啦~~~~此次上线的美国服务器包含美国云服务器、美国服务器、美国高防服务器以及美国高防云服务器。针对此次美国服务器新品上线,华纳云也推出了史无前例的超低活动力度。美国云服务器低至3折,1核1G5M低至24元/月,20G DDos防御的美国服务器低至688元/月,年付再送2个月,两年送4个月,三年送6个月,且永久续费同价,更多款高性价比配置供您选择。...

香港、美国、日本、韩国、新加坡、越南、泰国、加拿大、英国、德国、法国等VPS,全球独立服务器99元起步 湘南科技

全球独立服务器、站群多IP服务器、VPS(哪个国家都有),香港、美国、日本、韩国、新加坡、越南、泰国、加拿大、英国、德国、法国等等99元起步,湘南科技郴州市湘南科技有限公司官方网址:www.xiangnankeji.cn产品内容:全球独立服务器、站群多IP服务器、VPS(哪个国家都有),香港、美国、日本、韩国、新加坡、越南、泰国、加拿大、英国、德国、法国等等99元起步,湘南科技VPS价格表:独立服...

转义字符为你推荐
机构apple路由器itunes日照职业技术学院RIZHAO支持ipadDeviceios5eacceleratoraccess violation问题的解决办法!重庆宽带测速重庆云阳电信宽带测速网址谁知道,帮个忙?ipadwifiIPAD连上了WIFI,但是无法上网,急!!127.0.0.1为什么输入127.0.0.1无法打开页面ipad上网ipad上网速度很慢怎么回事?
长沙虚拟主机 已经备案域名 cybermonday 主机 windows主机 日本空间 cpanel空间 linux空间 php空间购买 电信网络测速器 日本代理ip 服务器论坛 江苏徐州移动 江苏双线 杭州电信 国外代理服务器 广州主机托管 闪讯网 webmin windowsserver2012 更多