數(shù)據(jù)類型轉(zhuǎn)換
//static_cast<int>(10.1) 靜態(tài)轉(zhuǎn)換 不適應(yīng)于指針 <>要轉(zhuǎn)換的類型 ()要轉(zhuǎn)換的數(shù)據(jù)
//reinterpret_cast<void>( pchar+i) 用于指針類型轉(zhuǎn)換 C語言 直接賦值
//涉及const 必須要用 const_cast<int>
//dynamic_cast<huahua*>(pzi); //dynamic_cast用于安全的轉(zhuǎn)換父類對(duì)象與子類對(duì)象 如果不匹配 轉(zhuǎn)換失敗為null
//主要用于多態(tài) 依賴于虛函數(shù)
//父類
class huahua
{
public:
int hua1;
virtual void run()
{
cout << "hhhhhhh" << endl;
}
};
//子類
class xiaohua:public huahua
{
public:
int xiaohua1;
void run()
{
cout << "xxxxhhhhhhh" << endl;
}
};
void mainr1()
{
//printf("%d",10.1); //垃圾值 printf不會(huì)完成數(shù)據(jù)類型轉(zhuǎn)換
//printf("%d", static_cast<int>(10.1)); //默認(rèn)轉(zhuǎn)換
//int*pint = new int(1);
//char*pchar = reinterpret_cast<char*>(pint); //指針類型轉(zhuǎn)換
//for (int i = 0; i < 3; i++)
//{
// cout << static_cast<int>(*(pchar + i)) << " "<<reinterpret_cast<void*>( pchar+i) <<endl;
//}
int num[5]{ 1,2,3,4,5 };
const int *p = num;
//int*pint = reinterpret_cast<int*>(p); // reinterpret_cast 不適用于const
int*pint = const_cast<int*>(p);
//for (size_t i = 0; i < 5; i++)
//{
// cout << pint[i] << " " << reinterpret_cast<void*>(pint + i) << endl;
//
//}
//父類指針可以經(jīng)過轉(zhuǎn)換訪問子類對(duì)象 反之一般不安全
huahua *pfu = new huahua;
xiaohua *pzi = new xiaohua;
huahua *phua = dynamic_cast<huahua*>(pzi); //子類對(duì)象轉(zhuǎn)換為父類指針
phua->run();
xiaohua *pxiaohua = dynamic_cast<xiaohua*>(pfu); //轉(zhuǎn)換失敗 會(huì)設(shè)置為null
if (pxiaohua != nullptr)
{
pxiaohua->hua1 = 10; //訪問子類繼承父類的對(duì)象
pxiaohua->run();
(*pxiaohua).huahua::run(); //調(diào)用子類中的父類的方法
}
cin.get();
}
delete刪除一個(gè)函數(shù)
void show(int num)
{
cout << num << endl;
}
void show(char num) = delete; //刪除一個(gè)函數(shù)
//函數(shù)聲明=delete
// 精確匹配 刪除其他參數(shù)匹配類型函數(shù)
class myclass
{
public:
//myclass() = delete; //刪除構(gòu)造函數(shù)
~myclass() = delete;
};
大括號(hào)處理數(shù)據(jù)
#include<initializer_list>
void show(initializer_list<int>list)
{
for (auto i : list)
{
cout << i << endl;
}
}
void show(initializer_list<char*>list)
{
for (auto i : list)
{
cout << i << endl;
}
}
int*getmem(initializer_list<int>list)
{
int length = list.size();
int *p = new int[length];
int*pback = p; //備份指針
for (auto i : list)
{
*(p++) = i; //循環(huán)賦值
}
return pback;
}
void mainr2()
{
show({ 1,2,3,4,5,6 });
show({"DDD","eeee","FFFFF","dddd"});
int*p = getmem({ 1,2,3,4,5,6 });
for (size_t i = 0; i < 6; i++)
{
cout << p[i] << " ";
}
cout << endl;
cin.get();
}
forward_list
#include<forward_list>
void mainr3()
{
forward_list<int>list1{ 1,2,3,4,5 };
forward_list<int>list2{ 1,2,3,4,5 };
//list1.push_front(10); //只能頭插
list2.assign(10,19); //分配并初始化
//for (auto i : list1)
//{
// cout << i << endl;
//}
//auto it = list1.before_begin(); //鏈表頭結(jié)點(diǎn)
//list1.insert_after(it, 18); //鏈表的插入
auto ib = list1.begin();
list1.insert_after(ib, 19);
int a[5]{ 1,2,3,4,5 };
list1.insert_after(ib, a, a + 5); //插入
list1.erase_after(++ib); //刪除
list1.sort();
list2.sort();
list1.merge(list2);//歸并
cout << list1.max_size() << endl; //最大
list1.reverse(); //反轉(zhuǎn)
for (auto ib = list1.begin(), ie = list1.end();ib != ie;ib++)
{
cout << *ib << endl;
}
cin.get();
}
if_exits
#include<iostream>
#include<list>
//類模板
template<class T>
class Tclass
{
public:
T t;
void show(T tx)
{
cout << tx << endl;
}
};
void main()
{
//int num;
Tclass<int> t1;
list<int> mylist; //檢測某個(gè)模板有沒有根據(jù)類型實(shí)例化
_if_exists(Tclass<int>) //如果存在 執(zhí)行塊語句 可以判斷變量 函數(shù) 類型
{
cout << "num is" << endl;
}
_if_not_exists(Tclass<int>)
{
cout << "num no" << endl;
}
cin.get();
}
引用包裝器
void change(int &num)
{
num = 3;
}
template<class T>
void show(T t) //函數(shù)模板 改成引用
{
cout << t << endl;
t++;
}
void main()
{
double db(1);
double &rdb(db);
show(ref(db)); //ref 引用包裝器 讓模板識(shí)別出引用
cout << db << endl;
cin.get();
}