2018-05-19

auto與decltye的區(qū)別

//typeid只能獲取類型 不能獲取 引用 常量
//auto無法區(qū)分常量變量 也無法區(qū)分引用變量
//decltype 可以獲取常量屬性,引用屬性
//auto不可以放在結(jié)構(gòu)體內(nèi)部

#include<vector>

void maiz1()
{
    const vector<int>myint{1,2,3,4,5};
    auto inta = myint[1];
    cout << typeid(inta).name() << endl;

    decltype(myint[1]) intd = myint[1];
    cout << typeid(intd).name() << endl;

    cin.get();
}

type_traits

#include<type_traits>

template<class T1,class T2>
void same(const T1 &t1,const T2&t2)
{
    cout << typeid(T1).name() << is_integral<T1>::value << endl;
    cout << is_same<T1, T2>::value << endl;
    //判斷模板的數(shù)據(jù)類型
}


//typename enable_if< is_same<T1,T2>::vlaue >::type *p =nullptr 默認(rèn)參數(shù)
//typename      enable_if< is_same<T1, T2>::vlaue >::type
// enable_if<    is_same<T1, T2>::vlaue     >::type

template<typename T1,typename T2>
void check_type1(const T1 &t1, const T2 &t2,
    typename enable_if< is_same<T1,T2>::vlaue >::type *p =nullptr)
{
    cout << " " << t1 << " " << t2 << endl;
    cout << "類型相同" << endl;
}

template<typename T1,typename T2>
void check_type1(const T1 &t1, const T2 &t2,
    typename enable_if<!is_same<T1, T2>::vlaue >::type *p = nullptr)
{
    cout << " " << t1 << " " << t2 << endl;
    cout << "類型不相同" << endl;
}


void mainz2()
{
    int i(10);
    int &ri(i);
    int &&rri(i + 3);
    cout << is_lvalue_reference<decltype(i)>::value << endl;
    cout << is_lvalue_reference<decltype(ri)>::value << endl;
    cout << is_rvalue_reference<decltype(ri)>::value << endl;
    //判斷是左值引用還是右值引用
    //is_rvalue_reference<decltype(ri)>::value 

    int a[5];
    int*p = a;
    cout << is_array<decltype(a)>::value << endl; //判斷是不是數(shù)組
    cout << is_array<decltype(p)>::value << endl;

    int num = 20;
    double db = 20;
    string str1;
    cout << is_integral<decltype(num)>::value << endl; 
    cout << is_integral<decltype(db)>::value << endl;
    cout << is_class<string>::value << endl;
    cout << is_class<decltype(str1)>::value << endl;
    //判斷數(shù)據(jù)類型

    same(11, 22);
    same(11.1, 22);
    same(3, "123");

    check_type1(10, 10);

    check_type1(1, 10.1);

    cin.get();
}

枚舉體

//C語言枚舉 本質(zhì)類型檢測不嚴(yán)格 允許類型不一致


//C語言枚舉 本質(zhì)類型檢測不嚴(yán)格 允許類型不一致
enum color {
    red,
    yellow,
    white,
    blue
};
//沒有賦值  默認(rèn)0-遞增  賦值從賦值開始遞增

//指定數(shù)據(jù)類型  枚舉名稱不可以重名  外部不可以 內(nèi)部不可以
enum coloros :char
{
    red1,
    yellow1,
    white1,
    blue1
};

enum class colorX:unsigned long 
{
    red2,
    yellow2,
    white2,
    blue2
};

enum class jun :int
{
    司令=10,
    軍長=9,
    師長

};

void mainz3()
{
    enum color color1(red);
    enum color color2(color::white);//初始化一般形式
    //color1 = 1;  //C++不可以 類型檢測嚴(yán)格

    colorX c1(colorX::red2);// 遵循嚴(yán)格

    cin.get();
}

占位參數(shù)


//預(yù)留參數(shù)接口 參數(shù)無法調(diào)用
void add(int a,int b,int) //C++允許有占位:  
{
  
}

template<class T>
void show(T a, T, T c) //預(yù)留接口  第二個參數(shù)無法調(diào)用
{

}

void mainz4()
{
    add(1, 2, 3);
    cin.get();
}

寄存器變量


void mainz5()
{
    register int num = 10;// register  對于CPP來說 只是建議
    cout << &num << endl; //CPP自動優(yōu)化到內(nèi)存  編譯器優(yōu)化  檢測到取地址 就會優(yōu)化為內(nèi)存變量
    //&num  //C語言無法取寄存器地址 

    cin.get();
}

CPP左值右值自動轉(zhuǎn)換


void mainz6()
{
    
    //C++ 會把有內(nèi)存實體的右值轉(zhuǎn)換為左值
    int i(3);
    (++i) = 13;
    (i = 2) = 6;
    cout << i << endl;

    cin.get();
}

共用體

//節(jié)約內(nèi)存
//CPP共用體 可以初始化 內(nèi)部可以有函數(shù)
//共用體 共享內(nèi)存 不計入代碼區(qū)大小
//union 默認(rèn)是公有 可以設(shè)置為私有
//可以實現(xiàn)封裝 只有一個變量
//共用體無法繼承 多態(tài)

union MyUnion
{
    int num;
    double data;
    void go()
    {
        cout << "dd" << endl;
    }
};

void mainz7()
{
    MyUnion my1{ 14 }; //只能初始化一個變量
    cout << my1.num << endl;
    my1.data = 10.55;
    cout << my1.num << endl;
    cin.get();
}

硬盤模式查詢文件流

#include<fstream>
#include<memory>
#include<string>
#include<cstring>
#include<cstdlib>
//C++文件 ifstream ofstream
//cin cout  fin fout  >> <<適用于屏幕 文件
//string
void mainz8()
{
    ifstream fin("E:\\New\\視頻\\智峰互聯(lián)\\資料\\大數(shù)據(jù)相關(guān)數(shù)據(jù)\\kaifang.txt");
    ofstream fout(R"(C:\res.txt)");
    if (!fin || !fout)
    {
        cout << "文件打開失敗" << endl;
        return;
    }

    //char name[300]{ 0 };
    string name;
    cin >> name;

    while (!fin.eof()) //沒有到文件末尾 就繼續(xù)
    {
        //char str[500]{ 0 };
        //fin.getline(str,500); //讀取一行
        //char*p = strstr(str, name);

        string str;
        int pos = str.find_first_of(name);
        //.find_first找到第一個相同的字符  find查找整體

        //fin.getline(const_cast<char*>(str.c_str()) , 500);

        if (pos != -1)
        {
            cout << str << endl;
            fout << str << endl; //打印到屏幕 寫入到文件
        }
    }


    fin.close();
    fout.close(); //關(guān)閉文件
    system(R"(C:\res.txt)");
    cin.get();
}

內(nèi)存模式查詢文件流


#include<vector>
#include<fstream>  //文件流
#include<memory>

vector<string> g_all; //動態(tài)數(shù)組  每一個元素都是字符串
int i = 0;

//效率低 因為STL有很多優(yōu)化

void loadmem()
{
    ifstream fin("E:\\New\\視頻\\智峰互聯(lián)\\資料\\大數(shù)據(jù)相關(guān)數(shù)據(jù)\\kaifang.txt");
    if (!fin )
    {
        cout << "文件打開失敗" << endl;
        return;
    }

    while (!fin.eof()) //沒有到文件末尾 就繼續(xù)
    {
        char str[500]{ 0 };
        fin.getline(str, 500);//讀取一行
        string putstr;
        putstr += str; //生成C++字符串
        g_all.push_back(putstr);  //壓入

        if (i++ > 10000)
        {
            break;
        }

    }

    fin.close();

}


void search()
{
    while (1)
    {
        string str;
        cin >> str;
        for (auto i : g_all)
        {
            int pos = i.find(str,0); //查找
            if (pos != -1)
            {
                cout << i << endl;
            }
        }
    }
}

void mainz9()
{
    loadmem();
    search();

    cin.get();
}

CPP結(jié)構(gòu)體

//1 C++結(jié)構(gòu)體可以為空 C不可以
//2 C++結(jié)構(gòu)體可以有默認(rèn)值 C不可以
//3 C++結(jié)構(gòu)體定義變量無需關(guān)鍵字struct
//4 C++結(jié)構(gòu)體可以有函數(shù)
//5 變量是函數(shù)指針 是變量 是lamba函數(shù)塊 不計入體積
//6 結(jié)構(gòu)體默認(rèn)公有 可以設(shè)置為私有
//7 沒有私有變量情況下初始化方法
________//point p1{ 1,2 };
________ //point *p = new point{ 1,2 };

________ //point px[2] = { { 1,2 },{ 3,4 } };
________ //point *pj = new point[2]{ { 1,2 },{ 3,4 } };
________ //8 私有 需要構(gòu)造函數(shù) 按照這個風(fēng)格初始化
________ //pointit p11(1, 2);
________ //pointit *p11 = new pointit(3, 4); //指針

________ //pointit pss[3]{ pointit(3, 4) ,pointit(8, 4) ,pointit(5, 4) }; //數(shù)組
________ //pointit *pas = new pointit[3]{ pointit(3, 4) ,pointit(8, 4) ,pointit(5, 4) };//堆_________上數(shù)組
//9 結(jié)構(gòu)體的聲明 只能創(chuàng)建指針或者引用 擴展結(jié)構(gòu)體作用范圍
//10 結(jié)構(gòu)體內(nèi)部可以創(chuàng)建引用或者指針
//11 匿名結(jié)構(gòu)體 不可以初始化
//12 結(jié)構(gòu)體之間直接賦值 淺拷貝 只是賦值值 一般數(shù)據(jù) 指針(不復(fù)制指向的地址)
//13 賦值 無論私有還是公有都能拷貝

struct mystructX
{
    int num = 3;
    function<void(void)> fun = []() {};
    //auto fu  //結(jié)構(gòu)體無法保存auto

};

struct mystructXX
{

    void(*p)() = []() {};  //指針4字節(jié)

    function<void(void)> fun = []() {};  //40字節(jié)
    function<void(int)> fun1 = [](int a) {};
    function<void(char*)> fun2 = [](char*str) {};

};


struct pointl; //結(jié)構(gòu)體的聲明 只能創(chuàng)建指針或者引用

struct point
{
    int x;
    int y;
    struct point*p;
    struct point& rp; //結(jié)構(gòu)體內(nèi)部可以創(chuàng)建引用或者指針

};
struct pointit
{
public:
    pointit(int a,int b):x(a),y(b)
    {
      
    }
private:
    int x;
    int y;
};

struct wu
{
    virtual void show()
    {
        cout << "老子" << endl;
    }
};
struct  xiaowu:public wu
{
    void show()
    {
        cout << "小子是大爺" << endl;
    }
};

void mainz10()
{
    //fun 包裝器  一個40個字節(jié)
    //cout << sizeof(mystructXX) << endl;
    point p1{1,2};
    point *p = new point{ 1,2 };
    
    point px[2] = { {1,2},{3,4} };
    point *pj = new point[2]{ { 1,2 },{ 3,4 } };
     
    pointit p11(1, 2);
    pointit *p11 = new pointit(3, 4); //指針

    pointit pss[3]{ pointit(3, 4) ,pointit(8, 4) ,pointit(5, 4) }; //數(shù)組
    pointit *pas=new pointit[3]{ pointit(3, 4) ,pointit(8, 4) ,pointit(5, 4) };//堆上數(shù)組

    p1.x = p1.y = 20;

    wu*pw = new wu; //多態(tài)
    pw->show();
    pw = new xiaowu;
    pw->show();


    cin.get();
}

異常

//異常與錯誤不一樣 異常 一般能夠正常工作
//錯誤就是程序無法正常工作 無法編譯
//異常讓程序在錯誤的輸入 文件不存在 內(nèi)存異常的情況下仍然可以正常工作
//try throw catch

int  divvv(int a, int b)
{

    try //嘗試
    {

        if (b == 0)
        {
            throw 1; //拋出異常  跳到catch
        }
        cout << a << " " << endl;
        return a / b;
    }
    catch (int code) //捕獲錯誤
    {
        if (code == 1)
        {
            cout << "被除數(shù)不可以為0" << endl;
        }

        return 0;
    }

}

void mainz11()
{

    int a, b;
    cin >> a >> b;
    divvv(a, b);

    cin.get();
}

CPP數(shù)據(jù)類型極限


#include<limits>

void mainz12()
{
    //cout << numeric_limits<int>::max() << endl;
    //cout << numeric_limits<int>::min() << endl;
    cout << numeric_limits<int>::lowest() << endl; //最小 整數(shù)含義一樣

    //cout << numeric_limits<double>::min() << endl; // 對于double  能表示最小精度的數(shù)
    //cout << numeric_limits<double>::lowest() << endl; //能表示最小的數(shù)

    cin.get();
}

算法容器函數(shù)


#include<functional>
#include<numeric>
#include<vector>
#include<algorithm>


using namespace std::placeholders;

template<class T>
bool getT(T data)
{
    return data % 2 == 0;
}


int get(int num)
{
    return num % 2 == 0;
}


struct  myx
{
    int get(int num)
    {
        return num % 2 == 0;
    }

};

struct XXXX
{
    int operator()(int num)
    {
        return num % 2 == 0;
    }
};

void mainz13()
{
    vector<int> myint{1,2,3,4,5,6,7,8,9,10};


    //計數(shù)滿足_Pred(第三個參數(shù))的元素
    int num = count_if(myint.begin(), myint.end(), [](int data) ->bool {return data % 2 == 0;});
    int num1 = count_if(myint.begin(), myint.end(),XXXX());
    XXXX x1;
    int num2 = count_if(myint.begin(), myint.end(), x1);
    int num3 = count_if(myint.begin(), myint.end(), get);
    int num4 = count_if(myint.begin(), myint.end(), get);

    myx my1;
    auto fun = bind(&myx::get,&my1,_1);
    int num5 = count_if(myint.begin(), myint.end(), fun);

    int num6 = count_if(myint.begin(), myint.end(),getT<int>);


    cout << num << endl;
    cout << num1 << endl;


    cin.get();
}

匿名對象與分配內(nèi)存時手動控制構(gòu)造與析構(gòu)

#include<memory>
#include<allocators>
#include<cstdlib>

class mycalssx
{
public:
    mycalssx()
    {
        cout << "mycalssx()" << endl;
    }

    mycalssx(const mycalssx&my)  //拷貝構(gòu)造函數(shù)
    {
        cout <<"const mycalssx()" << endl;
    }

    //mycalssx()
    //{
    //  cout << "mycalssx()" << endl;
    //}

private:

};


void mainz14()
{
    mycalssx*p = new mycalssx;


    allocator<mycalssx>my; //分配器  可以自動控制構(gòu)造 和 析構(gòu)的時間
    mycalssx*px = my.allocate(1);//分配一個元素
    my.construct(px,mycalssx()); //調(diào)用構(gòu)造函數(shù)  //mycalssx() 匿名對象 銷毀
    my.destroy(px);//釋放內(nèi)存  調(diào)用析構(gòu)
    my.deallocate(px,1); //直接釋放



    mycalssx*px3 = my.allocate(3);//分配3個元素
    my.construct(px3, mycalssx()); 
    my.construct(px3+1,mycalssx()); 
    my.construct(px3+2, mycalssx()); 
    my.destroy(px3);//釋放內(nèi)存  調(diào)用析構(gòu)
    my.destroy(px3+1);//釋放內(nèi)存  調(diào)用析構(gòu)
    my.destroy(px3+2);//釋放內(nèi)存  調(diào)用析構(gòu)


    ////匿名對象寄存器緩存
    //mycalssx(); //匿名對象  構(gòu)造完了 不保存 馬上析構(gòu)
    //mycalssx*p = new mycalssx(); //保存在堆上 就不在析構(gòu)
    //mycalssx f1 = mycalssx(); //創(chuàng)建對象

    int a(5); //原理就是構(gòu)造函數(shù)  ====  int a=int(5); 


    cin.get();
}

類默認(rèn)生成的四個函數(shù)

//類默認(rèn)生成4個函數(shù): 拷貝構(gòu)造 構(gòu)造 析構(gòu) 賦值重載
//myclass520 my1; myclass520 my2(my1); //拷貝構(gòu)造
//myclass520 *p=new myclass520; //構(gòu)造
//delete p; //析構(gòu)
//my1=my2; //賦值重載
//delete刪除
//default存在
//C++會給每個類生成四個函數(shù) 寫了新函數(shù)會覆蓋 default存在

class myclass520
{
public:
    //mycalss520(const myclass520&my) = delete;
    //myclass520() = delete;
    //void operator=(const myclass520&my)=default;
    //myclass520() =default;  //default 存在 聲明一下

    int x;
    int y;

    myclass520()
    {
        cout << "creat" << endl;
    }
    ~myclass520()
    {
        cout << "delete" << endl;
    }
    myclass520(const myclass520 &my)
    {
        x = my.x;
        y = my.y;
        cout << "拷貝構(gòu)造" << endl;
    }
    //myclass520 operator=(const myclass520 &my)
    void operator=(const myclass520 &my)
    {
        x = my.x;
        y = my.y;
        cout << "賦值重載" << endl;
    }


    void show()
    {
        cout << x << " " << y << endl;
    }

private:

};

class bobo
{
public:
    int x;
    int y;
    int z;
    //寫了構(gòu)造函數(shù)  往往會覆蓋原生
    bobo() = default;
    bobo(int a,int b,int c):x(a),y(b),z(c)
    {
    
    }
    ~bobo()
    {

    }

private:

};



void mainr15()
{
    //myclass520 my1; //調(diào)用構(gòu)造函數(shù)
    //myclass520 my2(my1);//調(diào)用拷貝構(gòu)造
    //myclass520 my3;
    //my3 = my1; //調(diào)用賦值重載

    myclass520 my1;
    my1.x = 10;my1.y = 20;
    my1.show();
    myclass520 my2(my1);
    my2.show();


    cin.get();
}

模板參數(shù)展開

#include<cstdarg>
template<class T>
void showit(T t)
{
    cout << t << endl;
}

template<class...Args>
void all(Args...args)
{
    int arr[] = { (showit(args),0)... };
}

template<class...Args>
void allit(Args...args)
{
    int arr[] = { (showit(args),0)... };
    //int arr[] 約束展開  不能省略
}


void mainz16()
{
    all(1, 2, 3, 4, 5,6);
    allit(1, 'a', "ssss", 7.8);
    cin.get();
}

轉(zhuǎn)義字符


#include<string>

void main()
{
    string str1(R"(12345\n455)");
    string str2(R"-(12345\n455"""12345\n455)-"); //-處理對稱性錯誤
    cout << str1 << endl;
    cout << str2 << endl;

    cin.get();
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.ios高性能編程 (1).內(nèi)層 最小的內(nèi)層平均值和峰值(2).耗電量 高效的算法和數(shù)據(jù)結(jié)構(gòu)(3).初始化時...
    歐辰_OSR閱讀 30,265評論 8 265
  • 307、setValue:forKey和setObject:forKey的區(qū)別是什么? 答:1, setObjec...
    AlanGe閱讀 1,724評論 0 1
  • 1.面向?qū)ο蟮某绦蛟O(shè)計思想是什么? 答:把數(shù)據(jù)結(jié)構(gòu)和對數(shù)據(jù)結(jié)構(gòu)進行操作的方法封裝形成一個個的對象。 2.什么是類?...
    少帥yangjie閱讀 5,125評論 0 14
  • 減肥對一個人的改變能有多大? 無論你打開一個社交網(wǎng)站/應(yīng)用,只要輸入“減肥”的關(guān)鍵字搜索,都能得到一個大寫的驚嘆號...
    寺變閱讀 393評論 0 0
  • 董卿首次擔(dān)任制作人的轉(zhuǎn)型之作《朗讀者》成為綜藝節(jié)目中的一股清流,豆瓣網(wǎng)上以9.5分高分得到了廣大網(wǎng)友的支持和認(rèn)可。...
    沐清小寨閱讀 863評論 0 0

友情鏈接更多精彩內(nèi)容