auto
#include<iostream>
using namespace std;
//auto自适应数据类型,自动推理
void main1()
{
//auto num = 10;//int
//auto num = 10.1;//double
//outo num = 'A';//char
auto num = L"bobo";
cout << typeid(num).name() << endl;
cin.get();
}
void main2()
{
int a[5] = { 1,2,3,4,5 };
for (auto i : a)//auto i是副本
{
i += 1;
cout << i << endl;//12345
}
for (auto &i : a)//加上&是原本
{
i += 1;
cout << i << endl;//12345
}
for (auto i : a)
{
cout << i << endl;//12345
}
system("pause");
}
template <class T1,class T2>
auto add(T1 t1, T2 t2)//自动推理数据类型
{
return t1 + t2;
}
void main()
{
cout << add(10, 11.1) << endl;
cout << add(10.1, 11) << endl;
system("pause");
}
array
#include<iostream>
#include<array>
using namespace std;
void main1()
{
//C风格数组
int a[5];
int b[5][5];
/*for (auto i : a)//不能用auto循环
{
for (auto j : i)
{
}
}*/
cin.get();
}
void main2()
{
array<int, 10>myint{ 1,2,3,4,5,6,7,8,9,10 };//CPP风格数组
for (auto i : myint)
{
cout << i <<" " << &i << endl;//i地址一样,有副本机制
}
//内存连续,在栈上
for (int i = 0; i < 10; i++)
{
cout << myint[i] << " " << &myint[i] << endl;//这样打印地址是正确的
}
cin.get();
}
void main()
{
//CPP风格二维数组
array<int, 10>myint1{ 12,2,33,42,5,6,7,8,9,10 };
array<int, 10>myint2{ 13,2,3,44,5,65,7,8,9,10 };
array<int, 10>myint3{ 1,22,3,4,35,6,47,8,9,10 };
array<array<int, 10>, 3>myint{ myint1 ,myint2 ,myint3 };//嵌套
for (auto j : myint)
{
for (auto i : j)
{
cout << i << " ";
}
cout << "\n";
}
//第二种打印方式
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 10; j++)
{
cout << myint[i][j] << " ";
}
cout << endl;
}
cin.get();
}
bool
#include<iostream>
using namespace std;
void main1()
{
//bool b = 3;//非0解析为1 0解析为0
bool b = true;//应该赋值为true或者false
cout << sizeof(b) << endl;//1个字节
cout << b << endl;//1
while (b)//死循环
{
cout << "haahha" << endl;
}
cin.get();
}
void main()
{
bool b1(false);//初始化
bool b1(true);
//b1 = 0 && 10 && -1 || 2 + 3;//+运算符优先,所以是死循环
//b1 = 0 && 10 && -1 || 2;// 0 && 10 && -1可以看做一个整体,0||2 还是死循环
b1 = 3 > 2 && 3 < 1 || 1 | 2;//|位或 肯定是非0 死循环
while (b1)
{
cout << "11111";
}
}
nullspr
#include<iostream>
using namespace std;
void show(int num)
{
cout << "int" << endl;
}
void show(int *p)
{
cout << "int *" << endl;
}
void main()
{
show(NULL);//int 会解释为一个整数0 C风格的空指针,会被误认为整数
show(nullptr);//int * C++风格空指针
cout << typeid(NULL).name() << endl;//int
cout << typeid(nullptr).name() << endl;//str::nullptr_t
//严格类型匹配
cin.get();
}
decltype
#include<iostream>
using namespace std;
void main1()
{
auto a1("hello");
auto b1(1 && 0 && 3 || -1);
cout << typeid(b1).name() << " " << b1 << endl;//bool 1
auto b2(b1);//auto可以实现拷贝,但是只能拷贝一个变量
cout << typeid(b2).name() << " " << b2 << endl;//bool 1
//decltype是一个关键字
//decltype(b1)根据一个变量表达式获取类型,创建数组,创建指针
decltype(b1) b3[5]{ 0 };
for (auto i : b3)
{
cout << typeid(i).name() << " " << i << endl;//bool 1
}
cin.get();
}
template<class T>
void show(T *p)
{
decltype(*p) num(*p);//内存备份
cout << num << endl;
cout << *p << endl;
}
void main2()
{
int num = 10;
show(&num);//10 10
}
template<class T>
void show1(T *p)
{
int a = *p;//获取一个类型
decltype(a) num[5]{ a,a ,a ,a ,a };
for (auto i : num)
{
cout << i << endl;
}
}
void main()
{
int num = 10;
show1(&num);
cin.get();
}