decltype: 用于推导表达式或者函数返回值
2.直接上代码
int main() { conststd::vector<int> v(1); autoa = v[0]; // a 的类型是 int decltype(v[0]) b = 1; // b 的类型是 const int&, 因为函数的返回类型是 // std::vector<int>::operator[](size_type) const autoc = 0; // c 的类型是 int autod = c; // d 的类型是 int decltype(c) e; // e 的类型是 int, 因为 c 的类型是int decltype((c)) f = c; // f 的类型是 int&, 因为 (c) 是左值 decltype(0) g; // g 的类型是 int, 因为 0 是右值 }3.auto的最主要用处是STL中的迭代子类型,使得代码简介; 使用前: std::map<std::string, std::vector<int>> map; std::map<std::string, std::vector<int>>::iterator it; for (it = map.begin(); it != map.end(); ++it) { } 使用后: std::map<std::string, std::vector<int>> map; for(auto it = begin(map); it != end(map); ++it) { } 4.扩展 获取auto的类型,使用typeid; #include <typeinfo> auto i = 10; cout<<typeid(i).name()<<endl;
