C++入门3
- C++新特性
 - auto推导规则
 - auto 作为函数的形参类型
 - decltype
 - 基于范围for循环
 
- typedef与using
 - C语言定义变量
 - typedef 在C语言中的写法
 - using在C++11中的写法
 - using与template的结合
 
- string的简单使用
 
C++新特性
auto推导规则
auto类型推导: auto定义的变量,可以根据初始化的值,在编译时推导出变量名的类型。c11中auto不再表示存储类型指示符,auto定义变量时,该变量一定给与初始化。
int main(){
	auto x = 5; // l ok x 是int类型
	auto pi = new auto(1);// okpi被推导为int * ;
	const auto *xp = &x,u = 6;l// ok xp是const int*类型,u是const int类型
	static auto dx = 0.0;l // ok dx 是 doub1e类型
	auto int b;
	// error c11中auto不再表示存储类型指示符
	auto s ;
	// error没有初始化值 auto无法推导出s的类型
 
连在一起写,容易出现二义性,最好分开写
intmain({
	auto x = 5;
	// ok x 是int类型
	const auto *xp = &x,u;
	//?
	const auto *ip = &x,u = 6.0; //?
	return 0;
}
 
和指针引用结合起来使用,const结合
int main({
	int x = 0 ;
	auto *ip = &x;// ok ip ->int*,auto被推导为int
	auto xp = &x ;// ok xp -> int*  ,auto被推导为int*
	auto &c = x;// ok c -> int &,auto被推导为int
	auto d = x;// ok d -> int , auto被推导为int
	const auto e = x;  ok e ->const int;
	autof = e;// okf -> int;
	const auto &g = x; // ok g -> const int &
	auto & h = g;// ok h -> const int &
}
 
auto 作为函数的形参类型
注意auto&x=ar,就是一个数组的别名,即
 int(&br)[5]=ar; auto 推导为int[5];他们等价
void func(auto x){
	cout << sizeof(x) << endl ;
	cout << typeid(x ).name() << endl ;
}
int mainO{
	int a = 10;
	int ar[]={12,23,34,45,56};
	func(a);//  4 int
	func(ar);// 4 int*    退化成指针
}
//**************************************
void func(auto &x){
	cout << sizeof(x) << endl ;
	cout << typeid(x).name( << endl ;
}
int main({
	int a = 10;
	int ar[={12,23,34,45,56};
	func(a);//4  int
	func(ar);//20  int [5]
 
注意:auto 不能在结构体使用,auto无法定义数组;
int main(){
	int ar[10]={0};
	auto br=ar;//br->int*
	auto cr[10]=ar;//error   
	auto dr[5]={1,2,3,4,5};//error
 
decltype
exp表示一个表达式(expression) 。
 从格式上来看,decltype很像sizeof-—用来推导表达式类型大小的操作符。类似于sizeof,decltype 的推导过程是在编译期完成的,并且不会真正计算表达式的值。
int main({
	int x = 10;
	decltype(x) y = 1;// y => int
	decltype(x+y) z = 0; // y = int;
	const int &i = x;
	decitype(i)j= y;// j = const int & ;
	const dec1type(z) *p = &z;//*p => const int , p = const int *
	dec1type (z) *ip = &z; // *ip => int , ip => int *
	dec1type(pi) *pp = &ip // *pp => int *,pp => int ;
 
基于范围for循环
for(ElemType val : array){
	..// statement循环体
}
 
ElemType 一般写成auto,让编译器自己推演,array是容器或者数组,不能是结构体
 容器有vector array list map unordered_map set unordered_set
int main(){
	int ar[]={1,2,3,4,5,6,7,8,9,10};
	int*p=ar;
	auto&x=ar;//等价int(&br)[10]=ar;
	int(&br)[10]=ar;
	//下面哪个不能编译通过:
	for(auto&val:p){}//error:后面必须是数组或者容器
	for(auto&val:x){}//x是ar的别名
	for(auto&val:br){}//br是ar的别名
 
核心用法,可以打印一个二维数组
int main(){
	int ar[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
	for(auto&x:ar){//x就是一个数组的别名
		for(auto val:x){
			cout<<val<<endl;
		}
	}
}
 
打印了三个地址
int main() {
	int ar[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
	for (auto x : ar) {//x是一个地址,ar[0]  ar[1]  ar[2]
		cout << x<<' ';
		cout << endl;
	}
}
 
typedef与using
C语言定义变量
typedef 可以把一切合法的变量的定义改成类型名
struct Node{
	char s_id[10];
	int s_age
}
struct Node Node;
unsigned int u_int;
int array[10];
int* PINT;
int (*pfun)(int) ;
 
可以把结构体合二为一
typedef struct Node{
	char s_id[10];
	int s_age
}Node;
 
typedef 在C语言中的写法
typedef struct Node{
	char s_id[10];
	int s_age
}
typedef unsigned int u_int;
typedef int array[10];
typedef int* PINT;
typedef int (*pfun)(int) ;
typedef struct Node Node;
int main ()
{
	u_int x,y;
	array ar,br;
	PINT xp,yp;
	pfun pf1,pf2;
	Node n1,n2;
}
 
using在C++11中的写法
typedef struct Node{
	char s_id[10];
	int s_age
}
using u_int= unsigned int ;
using array= int[10];
using PINT=int*;
uisng pfun=int(*)(int) ;//函数指针
using Node=struct Node;
int main ()
{
	u_int x,y;
	array ar,br;
	PINT xp,yp;
	pfun pf1,pf2;
	Node n1,n2;
}
 
using与template的结合
<>里面是什么类型,就定义什么类型的指针
template<class T>
using Pointer=T*;
int main(){
	int a=10;
	Pointer<int>p=&a;//p是整型指针
	return 0;
}
 
string的简单使用
在其他文章里详细介绍了C++常用的字符串:
 链接: C++字符串
int main(){
	char str[20]={"yhping"};
	str[0]='x';
	const char str1="yhping";
 
int main (){
	char stra[20] = "yhping" ;
	char strb[20] = "yhping";
	const char* cp1 = "yhping" ;
	const const*cp2 = "yhping";
	cout<<(stra == strb) << endl ;
	cout<< (cp1==cp2) << endl;
	return 0;
 
输出答案为0 1
 
                















![【LeetCode】数据结构题解(6)[回文链表]](https://img-blog.csdnimg.cn/52ade4d5b8c6462388f2aba13525c948.png#pic_center)


