C++类(五)—— 重新审视auto、比较三种for循环的效率、自定义的类如何使用新版本的 for(auto i : v)

容易犯的小错误 

void autoValue(bool age) {
	//此时,该age会将传入的参数age覆盖掉
	auto age = 10;	
}

auto的使用范例

static void autoValue() {
	auto age = 10;
	auto name = std::string("Yt");
	auto height = 160.0f;
	auto weight = 72.0;
	//typeid是C++中的关键字。typeid的输出结果与平台相关
	std::cout << "age is type " << typeid(age).name() << std::endl;
	std::cout << "name is type " << typeid(name).name() << std::endl;
	std::cout << "height is type " << typeid(height).name() << std::endl;
	std::cout << "weight is type " << typeid(weight).name() << std::endl;
}

static void autoPointer() {
	auto age = new int(10);  //age类型  int*
	auto name = "Yt";  //name的类型为const char*
	auto height = new float(160.0f);
	auto weight = new double(72.0);

	std::cout << "age is type " << typeid(age).name() << std::endl;
	std::cout << "name is type " << typeid(name).name() << std::endl;
	std::cout << "height is type " << typeid(height).name() << std::endl;
	std::cout << "weight is type " << typeid(weight).name() << std::endl;
}

 

 


比较三种for循环的效率(for (auto v : group) 、for (size_t i = 0; i < group.size();  ++i) 、for (std::vector::const_iterator iter = group.begin();iter != group.end(); ++iter))

static void newVersionFor() {

	int ids[] = { 1, 2, 3, 4, 5 };
	std::cout << "new version ";
	for (auto v : ids) {
		std::cout << v << " ";
	}
	std::cout << std::endl;  // "\n";

	std::cout << "old version ";
	// old version
	for (int i = 0; i < sizeof(ids) / sizeof(int); ++i) {
		std::cout << ids[i] << " ";
	}
	std::cout << std::endl;  // "\n";

	std::vector group;
	for (int i = 0; i < 4; ++i) group.push_back(i);
	/*
	从效率上分析这三种遍历:最快的是for (auto v : group) 

	for (size_t i = 0; i < group.size();  ++i) 
	每次循环都要求一下group.size(),进入循环之后还要找一下group[i]的位置

	for (std::vector::const_iterator iter = group.begin();
		iter != group.end(); ++iter) {
		std::cout << *iter << " ";
	}
	每次循环的时候都要比较一下iter != group.end(),
	并且还要把相应位置的值(即*iter)提取出来

	*/

	// auto version
	for (std::vector::const_iterator iter = group.begin();
		iter != group.end(); ++iter) {
		std::cout << *iter << " ";
	}
	std::cout << std::endl;

	for (size_t i = 0; i < group.size(); ++i) {
		std::cout << group[i] << " ";
	}
	std::cout << std::endl;

	std::cout << "vector verion : ";
	for (auto v : group) {
		v = v * v;
	}
	std::cout << std::endl;

	for (auto& v : group) { //auto求每个元素引用的例子
		v = v * v;
	}

	//const引用可以防止对传入的参数进行误操作。防止不小心把参数的值修改
	for (const auto& v : group) {}
}

 

自定义的类如何使用新版本的 for(auto i : v)

//自定义的类如何使用新版本的for(auto i : v)
class MyVector {
public:
	using GroupType = std::vector;
	typedef std::vector GroupType;
	//只需要定义一下这两个就可以了
	GroupType::iterator begin() { return m_group.begin();}
	GroupType::iterator end() { return m_group.end();}
private:
	GroupType m_group;
};

或者可以这么写

class MyVector {
public:
	using GroupType = std::vector;
	typedef std::vector GroupType;
	//GroupType::iterator begin() { return m_group.begin();}
	//GroupType::iterator end() { return m_group.end();}

	GroupType m_group;
};

MyVector::GroupType::iterator begin(MyVector& v) {
	return v.m_group.begin();
}

MyVector::GroupType::iterator end(MyVector& v) {
	return v.m_group.end();
}

 

你可能感兴趣的:(C++基础)