C++ 常用类型(auto、类型转换、decltype)

1 类型转换

1.1 string转换成int、double、float等类型

基于c++11标准,<string>中已经封装好了对应的转换方法

#include<string>
using namespace std;//添加名称空间

string d="123";
double num=stod(d);//string 转换double
int num=stoi(d);//string 转换成int
float num=stof(d);//string 转换成float
long num=stol(d);//string 转换成long

2 auto 

C++ 引入auto 和decltype关键字实现类型推导。在C++11标准的语法中,auto被定义为自动推断变量的类型。auto可以在声明变量的时候根据变量初始值的类型自动为此变量选择匹配的类型。

2.1使用原则

  • auto声明的变量必须要初始化,否则编译器不能判断变量的类型。这类似于const关键字。
  • auto不能被声明为返回值,auto不能作为形参,auto不能被修饰为模板参数。

2.2 应用举例 

int x = 0;
auto * a = &x;// a -> int * ,auto 被推导为int
auto b = &x;// b -> int * ,auto 被推导为int*
auto & c = x;// c -> int & ,auto 被推导为int
auto d = c;//d -> int,auto 被推导为int 当表达式是一个引用类型时,auto会把引用类型抛弃,直接推导成原始类型int
const auto e = x; // e -> const int ,auto 被推导为int
auto f = e; // f -> int ,auto 被推导为int,当表达式带有const属性时,auto会把const属性抛弃
const auto& g = x;//g -> const int& ,auto 被推导为int 当auto 和引用结合时,auto的推导结果将保留表达式const属性
auto& h = g;// h -> const int&  ,auto 被推导为const int

(1)在不声明为引用或指针时,auto会忽略等号右边的引用类型和const 和volatile限定。

(2)在声明为引用或者指针时,auto会保留等号右边的引用和const 和volatile属性

(3) auto关键字更适用于类型冗长复杂、变量使用范围专一时,使程序更清晰易读。

std::vector<int> vect; 
 for(auto it = vect.begin(); it != vect.end(); ++it)
 {  //it的类型是std::vector<int>::iterator
    std::cin >> *it;
}

(4) 保存lambda表达式类型的变量声明

auto ptr = [](double x){return x*x;};//类型为std::function<double(double)>函数对象

2.3 使用限制

 (1)auto 不能用于函数参数

void func(auto a = 1){}

(2)auto 不能用于非静态成员变量

class A
{
  auto a = 1;        // error,在类中auto不能用作非静态成员变量
  static auto b = 1;     // error,这里与auto无关,正常static int b = 1也不可以
  static const auto c = 1; // ok
};

(3)auto需马上初始化


 auto a;     //ERROR 无法推导auto类型,需要初始值设定

(4)auto在一行定义多个变量时,各个变量的推导不能产生二义性,否则编译失败

​auto d = 0, f = 1.0; // error,0和1.0类型不同,对于编译器有二义性,没法推导​​

 (5)auto不能定义数组,但可定义指针

int arr[10] = {0};
auto aa = arr; //ok ,aa -> int *;
auto rr[10] = arr;//error,auto无法定义数组

(6)auto无法推导出模板参数

vector<int> d;
vector<auto> f = d; // error,auto无法推导出模板参数

3 decltype

 decltype是C++11新增的一个关键字,和auto的功能一样,用来在编译时期进行自动类型推导。decltype则用于推导表达式类型,这里只用于编译器分析表达式的类型,表达式实际不会进行运算。

3.1 语法格式

decltype(exp)

 在编译时期,推导出表达式exp的类型。

3.2 使用规则

  • 如果exp是一个不被括号()包围的表达式,或者是一个类成员访问表达式,或者是一个单独的变量,decltype(exp)的类型和exp一致;
  • 如果exp是函数调用,则decltype(exp)的类型就和函数返回值的类型一致;
int& fun_int_l(void);//左值
int&& fun_int_rr(void);//右值
int fun_int(void);//纯右值
int x = 0;
decltype(fun_int_l()) a1 =x;//a1 -> int&
decltype(fun_int_rr()) b1 =0;//b1 -> int&&
decltype(fun_int()) c1 =0;//a1 -> int
  • 如果exp是一个左值,或被括号()包围,decltype(exp)的类型就是exp的左值引用,假设exp的类型为T,则decltype(exp)的类型为T&;
int x = 0;
decltype(x) y = 1;           // y -> int
decltype(x + y) z = 0;       // z -> int
const int& i = x;
decltype(i) j = y;           // j -> const int &
const decltype(z) * p = &z;  // *p  -> const int, p  -> const int *
decltype(z) * pi = &z;       // *pi -> int      , pi -> int *
decltype(pi)* pp = &pi;      // *pp -> int *    , pp -> int * *

3.3 decltype与auto 的不同点

auto 只能根据变量的初始化表达式推导出变量应该具有的类型。若要想通过某个表达式得到的类型,但又不希望新变量和这个表达式具有同样的值,此时auto不适用。

参考文献:

【1】C++ auto用法及应用详解_代码乌龟的博客-CSDN博客_c++ auto

【2】C++11 auto_aimalove的博客-CSDN博客 

【3】C++ 面试八股文_51CTO博客_java面试八股文 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值