模板引用内部函数绑定机制,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() ;//开始执行

CloudCone:洛杉矶MC机房KVM月付1.99美元起,支持支付宝/PayPal

CloudCone是一家成立于2017年的国外VPS主机商,提供独立服务器租用和VPS主机,其中VPS基于KVM架构,多个不同系列,譬如常规VPS、大硬盘VPS等等,数据中心在洛杉矶MC机房。商家2021年Flash Sale活动继续,最低每月1.99美元,支持7天退款到账户,支持使用PayPal或者支付宝付款,先充值后下单的方式。下面列出几款VPS主机配置信息。CPU:1core内存:768MB...

HostYun:联通AS9929线路,最低月付18元起,最高500Mbps带宽,洛杉矶机房

最近AS9929线路比较火,联通A网,对标电信CN2,HostYun也推出了走联通AS9929线路的VPS主机,基于KVM架构,开设在洛杉矶机房,采用SSD硬盘,分为入门和高带宽型,最高提供500Mbps带宽,可使用9折优惠码,最低每月仅18元起。这是一家成立于2008年的VPS主机品牌,原主机分享组织(hostshare.cn),商家以提供低端廉价VPS产品而广为人知,是小成本投入学习练手首选。...

BuyVM商家4个机房的官方测试IP地址和测速文件

BuyVM 商家算是有一些年头,从早年提供低价便宜VPS主机深受广大网友抢购且也遭到吐槽的是因为审核账户太过于严格。毕竟我们国内的个人注册账户喜欢账户资料乱写,毕竟我们看英文信息有些还是比较难以识别的,于是就注册信息的时候随便打一些字符,这些是不能通过的。前几天,我们可以看到BUYVM商家有新增加迈阿密机房,而且商家有提供大硬盘且不限制流量的VPS主机,深受有一些网友的喜欢。目前,BUYVM商家有...

转义字符为你推荐
深圳做网站-确认收货手太快网店发来空箱子产业2014年2月25日支持ipadipadwifiipad的wifi打不开怎么办?win10445端口怎么样打开电脑10800端口iphonewifi苹果手机突然用不了Wi-Fi了谷歌sb在谷歌里搜SB为什么结果中第一个是百度micromediawww.macromedia.com 是什么网站ipad上不了网平板电脑 能连接网络不能上网fastreport2.5pm10和pm2.5的区别,pm10和pm2.5哪个危害大
vps试用 免费二级域名注册 广州服务器租用 新秒杀 瓦工 便宜服务器 hawkhost 好看的桌面背景图 搜狗12306抢票助手 东莞数据中心 免费dns解析 空间登陆首页 贵阳电信 域名转入 闪讯网 免备案jsp空间 magento主机 删除域名 hosts文件修改 侦探online 更多