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语言中定义别名的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();

}

转载于:https://www.cnblogs.com/gccbuaa/p/7082488.html

引用内部函数绑定机制,R转义字符,C++引用,别名,模板元,宏,断言,C++多线程,C++智能指针...相关推荐

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

     1.引用内部函数绑定机制 #include<iostream> #include<functional> usingnamespacestd; usingnamespac ...

  2. Rust是如何实现内存安全的--理解RAII/所有权机制/智能指针/引用

    不带自动内存回收(Garbage Collection)的内存安全是Rust语言最重要的创新,是它与其他语言最主要的区别所在,是Rust语言设计的核心. Rust希望通过语言的机制和编译器的功能,把程 ...

  3. 9.C++弱引用智能指针weak_ptr的用处

    weak_ptr也是一个引用计数型智能指针,但是它不增加对象的引用计数,即弱引用.与之相对,shared_ptr是强引用,只要有一个指向对象的shared_ptr存在,该对象就不会析构,直到指向对象的 ...

  4. Node.js:模块查找,引用及缓存机制

    1. Node.js的模块载入方式与机制 Node.js中模块可以通过文件路径或名字获取模块的引用.模块的引用会映射到一个js文件路径,除非它是一个Node内置模块.Node的内置模块公开了一些常用的 ...

  5. 探究.NET的bin引用程序集运行机制看.NET程序集部署原理

    探究.NET的bin引用程序集运行机制 看.NET程序集部署原理 新建一个最简单的网站,并引用使用程序集Nhibernate.dll,页面代码为       运行后输出的结果 .NET 程序集部署程序 ...

  6. 常亮左值引用可以绑定右值的原因

    相关文章: 为什么常量左值引用可以绑定到右值? 根据该问题的几位答主的回答,整理成个人的理解. 从设计初衷上讲 允许引用绑定非左值的初衷在于"让传值还是传引用成为函数本身的细节,调用者不用去 ...

  7. C++/C++11中左值、左值引用、右值、右值引用的使用

    C++的表达式要不然是右值(rvalue),要不然就是左值(lvalue).这两个名词是从C语言继承过来的,原本是为了帮助记忆:左值可以位于赋值语句的左侧,右值则不能. 在C++语言中,二者的区别就没 ...

  8. 【C++】Android (Light)RefBase-sp-wp引用计数-智能指针源码分析

    文章目录 1.RefBase简介 2.RefBase源码分析 3.RefBase使用注意事项 4.总结 1.RefBase简介 什么是RefBase?RefBase是Android中的一个C++类,用 ...

  9. C++ — 智能指针的简单实现以及循环引用问题

    http://blog.csdn.net/dawn_sf/article/details/70168930 智能指针 _________________________________________ ...

  10. C++(9)--裸指针、智能指针、引用

    指针 1.裸指针的基本概念 1.1 裸指针的声明*/初始化& 1.2 操作裸指针--间接运算符* 1.3 裸指针使用 demo--指向一个简单变量 1.4 空指针--nullptr 1.5 特 ...

最新文章

  1. 完全用Linux工作,抛弃windows
  2. 扫描到服务器的文件在哪个文件夹,云服务器的文件在哪个文件夹
  3. 经典C语言程序100例之十三
  4. 关于mysql的wait_timeout参数 设置不生效的问题
  5. linux wptmp文件分析,wordpress上传图片提示“缺少临时文件夹”的解决方法
  6. WinCE切换GPRS
  7. import win32com.client在python中报错及其解决办法
  8. python中‘/’和‘//’区别
  9. Java8新特性之Lambda
  10. HDU-1863-畅通工程(并查集)
  11. nodejs之处理GET请求
  12. Windows Mobile获取通话记录 C#
  13. getprivateprofilestring读不到数据_SpringBoot2.x系列教程66--Spring Boot整合分布式事务之数据库事务回顾
  14. 雷赛acc68c说明书_DMC2410C-A四轴通用型点位卡
  15. mac整站下载工具httrack
  16. 图像处理:图像二值化原理
  17. SWFUpload文件上传
  18. 05二项式系数Cnk.md
  19. 【CTSC2010】珠宝商(SAM)(点分治)(根号分治)
  20. 程序员泪流满面的图片

热门文章

  1. hadoop发行版本之间的区别
  2. Linux指令:top
  3. 结对-结对编项目作业名称-测试过程
  4. 全国省市区SQL语句(mysql)
  5. C语言 · 进制转换
  6. [算法题] 安排会议室——贪心算法的应用
  7. Android开发小结Part11:SQLite 通过.db文件导入已有数据库
  8. 我的 Visual C++ 6.0学习网站
  9. Unix 文件和目录
  10. c语言第11章ppt,C语言程序设计第11章xg.ppt