C++进阶7:c++11那些事

1. auto

1.typeid(*).name() -----(类型的查看)

l类型 显示
int i
float f
char* PKc (pointer–const–char)
int arr[] typeid(arr).name()
typeid(string).name() 好长的字符串(string);
typeid(vector).name() 好长的字符串(vector)
typeid(Simple).name() 类的话(*个字符)
auto it = vec.begin();cout << typeid(it).name() << endl; 好长的字符串(iterator)
func func

注意:
(1)auto 进行变量定义必须初始化;

auto m1= 10//必须要初始化;

(2)auto不能推导数组;

auto m4[] = {
   1,2,3,4,5,6};
 cout << typeid(m4).name() << endl;

2.基于范围的for循环

for(auto a:arr){
   
        cout << a << " ";
    }
    cout << endl;

3.定义迭代器

  vector<int> vec = {
   1,2,3};
    //vector<int>::iterator it = vec.begin();  //不推荐
    auto it = vec.begin(); //推荐
    cout << typeid(it).name() << endl;

(4) 定义函数指针

auto func = Func;
func();
cout << typeid(func).name() << endl;

完整代码见001——auto.cpp

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

class Simple /*final*/{
    // 禁止继承

};

void Func(){
   
    cout<< __func__<<endl;
}


int main(){
   
    // auto 自动推导变量类型
    int n = 10;
    cout << typeid(n).name() << endl;//i

    float f;
    cout << typeid(f).name() << endl;  //f

    const char* hello = "abc";
    cout << typeid(hello).name() << endl;  //PKc (pointer--const--char)

    int arr[] = {
   1,2,3,4,5};
    cout << typeid(arr).name() << endl;//Ai5(array_int_5)

    cout << typeid(string).name() <<<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值