C++-auto关键字

1、作为迭代器

使用前:

#include<string>
#include<vector>
int main()
{
    std::vector<std::string> vs;
    for (std::vector<std::string>::iterator i = vs.begin(); i != vs.end(); i++)
    {
        //...
    }
}

使用后:

#include<string>
#include<vector>
int main()
{
    std::vector<std::string> vs;
    for (auto i = vs.begin(); i != vs.end(); i++)
    {
        //..
    }
}

 2、函数类型推导

C++11:

template <typename U, typename V>
auto add(U a, V b) -> decltype(a + b);

C++14:

auto func(T parameters){
    return something;
}

auto是在编译器就能知道类型是啥的,而且不会降低编译速度,因为正常编译时就会知道类型

参考:https://zhuanlan.zhihu.com/p/343507680

 

posted @ 2019-03-27 11:12  朱小勇  阅读(129)  评论(0编辑  收藏  举报