auto
auto这是一个很小的语法,就是让编译器自动推测对象的类型,需要注意的是一定要保证编译器可以推测出此对象的类型,不能出现模棱两可的情况。不过话又说回来对于c++来说,时刻清除对象的类型,恐怕是读懂程序的基础,auto只是一种偷懒的做法,平时自己写代码的时候,不建议使用,能看懂别人写的代码就行。下面附一个小例子:
// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
using namespace std;
int main(){
int i = 6;
auto j = i;
cout << j << endl;
vector<int> v = {1,5,6,8,9,7};
// vector<int>::iterator it = find(v.begin(),v.end(),8);
auto it = find(v.begin(),v.end(),8);
cout << *it << endl;
return 0;
}
结果:
6
8