c++ stl auto基本用法

//c++ auto基本用法:
//auto的原理就是根据后面的值,来自己推测前面的类型是什么。
//auto的作用就是为了简化变量初始化,如果这个变量有一个很长很长的初始化类型,就可以用auto代替。

//注意点:
//1.用auto声明的变量必须初始化(auto是根据后面的值来推测这个变量的类型,如果后面没有值,自然会报错)
//2.函数和模板参数不能被声明为auto(原因同上)
//3.因为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
//4.定义在一个auto序列的变量必须始终推导成同一类型
//auto x1 = 5, x2 = 5.0, x3='r';  // This is too much....we cannot combine like this


//示例:

//std::vector<std::string> ve;
//std::vector<std::string>::iterator it = ve.begin();
//我们可以用atuo来代替那个初始化类型:
//auto it = ve.begin();

#include <iostream>
#include <cstring>
#include <string>
#include <stdio.h>
using namespace std;
extern int A;
extern int B;

int main()
{
    auto int x=0;
    register int y=0;   //暗示频繁调用
    int A=0;
    int B=0;
    static int count=0;
    char *ch=new char();
    sprintf(ch,"%d,%d,%d,%d,%d.",x,y,count,A,B);
    cout<<ch<<endl;
}
发布了187 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/aa804738534/article/details/104482008
今日推荐