auto與decltye的區(qū)別
//typeid只能獲取類型 不能獲取 引用 常量
//auto無(wú)法區(qū)分常量變量 也無(wú)法區(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語(yǔ)言枚舉 本質(zhì)類型檢測(cè)不嚴(yán)格 允許類型不一致
//C語(yǔ)言枚舉 本質(zhì)類型檢測(cè)不嚴(yán)格 允許類型不一致
enum color {
red,
yellow,
white,
blue
};
//沒(méi)有賦值 默認(rèn)0-遞增 賦值從賦值開(kāi)始遞增
//指定數(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,
軍長(zhǎng)=9,
師長(zhǎng)
};
void mainz3()
{
enum color color1(red);
enum color color2(color::white);//初始化一般形式
//color1 = 1; //C++不可以 類型檢測(cè)嚴(yán)格
colorX c1(colorX::red2);// 遵循嚴(yán)格
cin.get();
}
占位參數(shù)
//預(yù)留參數(shù)接口 參數(shù)無(wú)法調(diào)用
void add(int a,int b,int) //C++允許有占位:
{
}
template<class T>
void show(T a, T, T c) //預(yù)留接口 第二個(gè)參數(shù)無(wú)法調(diào)用
{
}
void mainz4()
{
add(1, 2, 3);
cin.get();
}
寄存器變量
void mainz5()
{
register int num = 10;// register 對(duì)于CPP來(lái)說(shuō) 只是建議
cout << &num << endl; //CPP自動(dòng)優(yōu)化到內(nèi)存 編譯器優(yōu)化 檢測(cè)到取地址 就會(huì)優(yōu)化為內(nèi)存變量
//&num //C語(yǔ)言無(wú)法取寄存器地址
cin.get();
}
CPP左值右值自動(dòng)轉(zhuǎn)換
void mainz6()
{
//C++ 會(huì)把有內(nèi)存實(shí)體的右值轉(zhuǎn)換為左值
int i(3);
(++i) = 13;
(i = 2) = 6;
cout << i << endl;
cin.get();
}
共用體
//節(jié)約內(nèi)存
//CPP共用體 可以初始化 內(nèi)部可以有函數(shù)
//共用體 共享內(nèi)存 不計(jì)入代碼區(qū)大小
//union 默認(rèn)是公有 可以設(shè)置為私有
//可以實(shí)現(xiàn)封裝 只有一個(gè)變量
//共用體無(wú)法繼承 多態(tài)
union MyUnion
{
int num;
double data;
void go()
{
cout << "dd" << endl;
}
};
void mainz7()
{
MyUnion my1{ 14 }; //只能初始化一個(gè)變量
cout << my1.num << endl;
my1.data = 10.55;
cout << my1.num << endl;
cin.get();
}
硬盤(pán)模式查詢文件流
#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 << "文件打開(kāi)失敗" << endl;
return;
}
//char name[300]{ 0 };
string name;
cin >> name;
while (!fin.eof()) //沒(méi)有到文件末尾 就繼續(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找到第一個(gè)相同的字符 find查找整體
//fin.getline(const_cast<char*>(str.c_str()) , 500);
if (pos != -1)
{
cout << str << endl;
fout << str << endl; //打印到屏幕 寫(xiě)入到文件
}
}
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; //動(dòng)態(tài)數(shù)組 每一個(gè)元素都是字符串
int i = 0;
//效率低 因?yàn)镾TL有很多優(yōu)化
void loadmem()
{
ifstream fin("E:\\New\\視頻\\智峰互聯(lián)\\資料\\大數(shù)據(jù)相關(guān)數(shù)據(jù)\\kaifang.txt");
if (!fin )
{
cout << "文件打開(kāi)失敗" << endl;
return;
}
while (!fin.eof()) //沒(méi)有到文件末尾 就繼續(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)體定義變量無(wú)需關(guān)鍵字struct
//4 C++結(jié)構(gòu)體可以有函數(shù)
//5 變量是函數(shù)指針 是變量 是lamba函數(shù)塊 不計(jì)入體積
//6 結(jié)構(gòu)體默認(rèn)公有 可以設(shè)置為私有
//7 沒(méi)有私有變量情況下初始化方法
________//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ù) 按照這個(gè)風(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)建指針或者引用 擴(kuò)展結(jié)構(gòu)體作用范圍
//10 結(jié)構(gòu)體內(nèi)部可以創(chuàng)建引用或者指針
//11 匿名結(jié)構(gòu)體 不可以初始化
//12 結(jié)構(gòu)體之間直接賦值 淺拷貝 只是賦值值 一般數(shù)據(jù) 指針(不復(fù)制指向的地址)
//13 賦值 無(wú)論私有還是公有都能拷貝
struct mystructX
{
int num = 3;
function<void(void)> fun = []() {};
//auto fu //結(jié)構(gòu)體無(wú)法保存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 包裝器 一個(gè)40個(gè)字節(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();
}
異常
//異常與錯(cuò)誤不一樣 異常 一般能夠正常工作
//錯(cuò)誤就是程序無(wú)法正常工作 無(wú)法編譯
//異常讓程序在錯(cuò)誤的輸入 文件不存在 內(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) //捕獲錯(cuò)誤
{
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; // 對(duì)于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};
//計(jì)數(shù)滿足_Pred(第三個(gè)參數(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();
}
匿名對(duì)象與分配內(nèi)存時(shí)手動(dòng)控制構(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; //分配器 可以自動(dòng)控制構(gòu)造 和 析構(gòu)的時(shí)間
mycalssx*px = my.allocate(1);//分配一個(gè)元素
my.construct(px,mycalssx()); //調(diào)用構(gòu)造函數(shù) //mycalssx() 匿名對(duì)象 銷毀
my.destroy(px);//釋放內(nèi)存 調(diào)用析構(gòu)
my.deallocate(px,1); //直接釋放
mycalssx*px3 = my.allocate(3);//分配3個(gè)元素
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)
////匿名對(duì)象寄存器緩存
//mycalssx(); //匿名對(duì)象 構(gòu)造完了 不保存 馬上析構(gòu)
//mycalssx*p = new mycalssx(); //保存在堆上 就不在析構(gòu)
//mycalssx f1 = mycalssx(); //創(chuàng)建對(duì)象
int a(5); //原理就是構(gòu)造函數(shù) ==== int a=int(5);
cin.get();
}
類默認(rèn)生成的四個(gè)函數(shù)
//類默認(rèn)生成4個(gè)函數(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++會(huì)給每個(gè)類生成四個(gè)函數(shù) 寫(xiě)了新函數(shù)會(huì)覆蓋 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;
//寫(xiě)了構(gòu)造函數(shù) 往往會(huì)覆蓋原生
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ù)展開(kāi)
#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[] 約束展開(kāi) 不能省略
}
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)-"); //-處理對(duì)稱性錯(cuò)誤
cout << str1 << endl;
cout << str2 << endl;
cin.get();
}