博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++类(五)—— 重新审视auto、比较三种for循环的效率、自定义的类如何使用新版本的 for(auto i : v)
阅读量:3748 次
发布时间:2019-05-22

本文共 3058 字,大约阅读时间需要 10 分钟。

容易犯的小错误 

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<int>::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();}

 

转载地址:http://fidsn.baihongyu.com/

你可能感兴趣的文章
几种常用的版本控制系统优缺点比较
查看>>
版本控制:SVN和GIT的一些使用感受
查看>>
RAID 技术全解
查看>>
DELL R720 服务器 RAID阵列卡配置介绍
查看>>
SVN服务器部署并实现双机同步及禁止普通用户删除文件
查看>>
SVN服务器搭建和使用(一)
查看>>
SVN服务器搭建和使用(二)
查看>>
SVN服务器搭建和使用(三)
查看>>
文件服务器的配置与管理(1) RAID技术
查看>>
文件服务器的配置与管理(2) 实现软RAID
查看>>
文件服务器的配置与管理(3) 共享文件夹的创建与使用
查看>>
TortoiseSVN新人使用指南
查看>>
visual svn使用教程
查看>>
版本控制系统客户端VisualSVN的安装与使用(for Visual Studio)
查看>>
小白教程:Visual Studio2017配置GitHub图文教程
查看>>
句柄和指针的区别
查看>>
VC编写的程序不能在其他机器上运行的解决方案
查看>>
VC编写的程序不能在其他机器上运行的解决方案(续)
查看>>
VS 中配置使用Visual SVN系列 一:SVN Server下载和安装
查看>>
VS 中配置使用Visual SVN系列 二:SVN Client(客户端)下载和安装
查看>>