天天看点

C++11 2.0 autoauto

auto

假如你不想写被创建对象的类型,或者你不知道等式返回值类型是什么,你可以使用auto关键字,让编译器自动确定变量类型。

代码示例:

#include <iostream>
#include <list>
#include <string>
using namespace std;

int main(){
/*
	/
	list<string> c;
	...
	list<string>::iterator ite;
	ite = find(c.begin(),c.end(),target);
	/
	//假如你不想写list<string>::iterator ite;这句话,那么你可以写成下面的语句
	///
	list<string>c;
	...
	auto ite = find(c.begin(), c.end(), target);
	///
	
	//但是不能写成下面的形式
	list<string>c;
	...
	auto ite
	ite = find(c.begin(), c.end(), target);
	*/
	list<string>c;
	c.push_back("张飞");
	c.push_back("刘备");
	c.push_back("关羽");
	c.push_front("赵云");
	auto ite = find(c.begin(), c.end(), "关羽");
	cout << *ite << endl;
}

	system("pause");
	return 0;
           

执行结果:

C++11 2.0 autoauto

继续阅读