04_仿函数、去转义字符、using 别名、智能指针、多线程、断言

来源:互联网 发布:涤纶低弹网络丝荣盛 编辑:程序博客网 时间:2024/06/01 11:46

  • 目录
    • 一仿函数
    • 二转义字符
    • 三using 别名 与c的typedef相同
    • 四模板元编程
    • 五智能指针自动释放内存
    • 六多线程
    • 七静态断言

【目录】

一、 仿函数 2
1、 仿函数, 2
二、 转义字符 2
1、 去掉转义字符R”()” 2
三、 using 别名 (与c的typedef相同) 3
四、 模板元编程 3
五、 智能指针 4
1、 auto_ptr—–依赖于原生的指针 4
2、 C11新型智能指针(推荐使用)—unique_ptr 5
六、 多线程 5
七、 静态断言 6

一、仿函数

  1、仿函数::就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了。
    ①.创建一个函数指针,引用一个结构体内部或者一个类内部的公有函数using namespace std::placeholders;

#include<iostream>#include<functional>//处理函数using namespace std::placeholders;struct MyStruct {    void add(int a) {        std::cout<<a<<std::endl ;    }    void add1(int a,int b) {        std::cout<<a+b<<std::endl ;    }    void add2(int a,int b,int c) {        std::cout<<a+b+c<<std::endl ;    }} ;int main( int argc , char** argv ) {    MyStruct my ;    /*仿函数,创建一个函数指针,引用一个结构体内部或者一个类内部的公有函数*/    /*auto自动变量,地址,函数指针,bind绑定    **第一个参数引用内部函数,绑定一个实体对象*/    auto fun1 = bind( &MyStruct::add , &my , _1 ) ;    fun1(2) ;    auto fun2 = bind( &MyStruct::add1 , &my , _1 ,_2 ) ;    fun2( 5 , 10 ) ;    auto fun3 = bind( &MyStruct::add2 , &my , _1 ,_2 , _3 ) ;    fun3( 6 , 7 ,8 ) ;    /*---------------------------------*/    MyStruct struct1;    /*创建函数指针,类结构体,数据私有,代码共享*/    /*函数通过调用,调用需要传递对象名进行区分*/    void(MyStruct::*p)(int a) = &MyStruct::add;    (my.add)(3) ;     (my.*p)(3) ;    std::cin.get() ;    return 0 ;}

二、转义字符

  1、去掉转义字符R()
    ①.注:放在括号中的内容将被去掉转义字符

R“(E:\Langue\C++\2014传智播客C++\02_C++就业\20140816\vedio)”

三、using 别名 (与c的typedef相同)

#include <iostream>namespace space   //隔离模板,避免冲突{    template<class T> using ptr = T*; //模板的简写}int add(int a, int b){    return a + b;}typedef  int(*ADD)(int a, int b);using  FUNC = int (*)(int a, int b);//别名using  co = std::ios_base::fmtflags;//using只可以用于简写数据类型void main(){    ADD p=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));/*指向int类型的指针*/    //std::cout << *pint << "   " << pint << std::endl;    std::cin.get();}

四、模板元编程

#include<iostream>//模板元吧运行时消耗的时间,在编译期间优化template<int N>struct data {    enum a{ res = data<N - 1>::a::res + data<N - 2>::a::res };};template<>struct data<1> {    enum a{res =1};};template<>struct data<2> {    enum a{res= 1 };};//1  1  2  3  5  7int getdata(int n) {    if (n==1 || n==2)   {        return 1;    }     else    {        return getdata(n - 1) + getdata(n - 2);    }}void main() {    const int myint = 40;    int num = data<myint>::res ; //<>内部不可以有变量    std::cout << num << std::endl;    std::cout << getdata(40) << std::endl;    std::cin.get();}//主要思想////利用模板特化机制实现编译期条件选择结构,利用递归模板实现编译期循环结构,模板元程序则由编译器在编译期解释执行。////优劣及适用情况////通过将计算从运行期转移至编译期,在结果程序启动之前做尽可能多的工作,最终获得速度更快的程序。也就是说模板元编程的优势在于:////1.以编译耗时为代价换来卓越的运行期性能(一般用于为性能要求严格的数值计算换取更高的性能)。通常来说,一个有意义的程序的运行次数(或服役时间)总是远远超过编译次数(或编译时间)。////2.提供编译期类型计算,通常这才是模板元编程大放异彩的地方。////模板元编程技术并非都是优点:////1.代码可读性差,以类模板的方式描述算法也许有点抽象。////2.调试困难,元程序执行于编译期,没有用于单步跟踪元程序执行的调试器(用于设置断点、察看数据等)。程序员可做的只能是等待编译过程失败,然后人工破译编译器倾泻到屏幕上的错误信息。////3.编译时间长,通常带有模板元程序的程序生成的代码尺寸要比普通程序的大,////4.可移植性较差,对于模板元编程使用的高级模板特性,不同的编译器的支持度不同。

五、智能指针(自动释放内存)

  1、auto_ptr—–依赖于原生的指针

#include <iostream>void main1() {    for (int i = 0; i < 10000000; i++) {        double *p = new double;//为指针分配内存        std::auto_ptr<double> autop(p);        //创建智能指针管理指针p指向内存        //智能指针         //delete p;    }    std::cin.get();}

  2、C11新型智能指针(推荐使用)—unique_ptr

#include<iostream>#include<memory>   //内存void main() {    for (int i = 0; i < 10000000;i++) {        //新型指针,新型的数组        std::unique_ptr<double> pdb(new double);        //double *p = new double;    }    std::cin.get();}

六、多线程

#include <thread>#include<iostream>#include<windows.h>#include<vector>using namespace std;using namespace std::this_thread;void msg() {    MessageBoxA(0, "12345", "678910", 0);}void msgA(int num) {    std::cout << get_id() << "  num=   " << num << std::endl;}void main1() {    // thread::hardware_concurrency查看计算机线程    auto n = thread::hardware_concurrency();    std::cout << n << std::endl;    //获取当前线程编号    std::cout << "thread=" << get_id() << std::endl;   thread thread1(msg);//创建多线程   thread thread2(msg);   thread1.join();//开始执行   thread2.join();    std::cin.get();}void main2() {    vector<thread *> threads;    for (int i = 0; i < 10; i++)    {        threads.push_back(new thread(msg));//创建线程    }    for (auto th : threads)     {        th->join();    }    std::cin.get();}void main() {    vector<thread *> threads;    for (int i = 0; i < 10; i++)    {        threads.push_back(new thread(msgA,i));//创建线程    }    for (auto th : threads)     {        th->join();    }    std::cin.get();}

七、静态断言

#include <stdio.h>#include<assert.h>#include<iostream>using namespace std;#define  N 10void main() {    int num = 100;    cout << num << endl;    cout << __FILE__ << endl;    cout << __LINE__ << endl;    cout << __DATE__ << endl;    cout << __TIME__ << endl;    cout << __FUNCTION__ << endl;    cin.get();}#define Mvoid main1() {   char  num = 10;    //字节>4#ifdef  M  // static_assert(sizeof(num) >= 4, "yincheng error");#endif    //调试代码,迅速代码错误在哪一行}//regex pattern("[[:digit:]]",regex_constants::extended);    //std::locale::global(std::locale("Chinese"));    //setlocale(LC_ALL, "chs");    /*    std::string s ("我是你爹,你把");      std::smatch m;      std::regex e ("你");   // matches words beginning by "sub"     while (std::regex_search (s,m,e)) {          for (auto x=m.begin();x!=m.end();x++)               std::cout << x->str() << ":: ";              std::cout << "--> ([^ ]*) match " << m.format("$0") <<std::endl;              s = m.suffix().str();      }      */
阅读全文
0 0
原创粉丝点击