当前位置: 首页 > news >正文

C++:基于范围的for循环

使用迭代器遍历容器在遍历的过程中需要给出容器的两端:开头(begin)和结尾(end),因为这种遍历方式不是基于范围来设计的。在基于范围的for循环中,不需要再传递容器的两端,循环会自动以容器为范围展开,并且循环中也屏蔽掉了迭代器的遍历细节,直接抽取容器中的元素进行运算,使用这种方式进行循环遍历会让编码和维护变得更加简便。

#include <iostream>
#include <vector>
using namespace std;int main(void)
{vector<int> t{ 1,2,3,4,5,6 };for (auto value : t){cout << value << " ";}cout << endl;return 0;
}

在上面的例子中,是将容器中遍历的当前元素拷贝到了声明的变量value中,因此无法对容器中的元素进行写操作,如果需要在遍历过程中修改元素的值,需要使用引用。

#include <iostream>
#include <vector>
using namespace std;int main(void)
{vector<int> t{ 1,2,3,4,5,6 };cout << "遍历修改之前的容器: ";for (auto &value : t){cout << value++ << " ";}cout << endl << "遍历修改之后的容器: ";for (auto &value : t){cout << value << " ";}cout << endl;return 0;
}

对容器的遍历过程中,如果只是读数据,不允许修改元素的值,可以使用const定义保存元素数据的变量,在定义的时候建议使用const auto &,这样相对于const auto效率要更高一些。

#include <iostream>
#include <vector>
using namespace std;int main(void)
{vector<int> t{ 1,2,3,4,5,6 };for (const auto& value : t){cout << value << " ";}return 0;
}

使用基于范围的for循环有一些需要注意的细节,先来看一下对关系型容器map的遍历:

#include <iostream>
#include <string>
#include <map>
using namespace std;int main(void)
{map<int, string> m{{1, "lucy"},{2, "lily"},{3, "tom"}};// 基于范围的for循环方式for (auto& it : m){cout << "id: " << it.first << ", name: " << it.second << endl;}// 普通的for循环方式for (auto it = m.begin(); it != m.end(); ++it){cout << "id: " << it->first << ", name: " << it->second << endl;}return 0;
}

在上面的例子中使用两种方式对map进行了遍历,通过对比有两点需要注意的事项:

使用普通的for循环方式(基于迭代器)遍历关联性容器, auto自动推导出的是一个迭代器类型,需要使用迭代器的方式取出元素中的键值对(和指针的操作方法相同):
it->first
it->second
使用基于范围的for循环遍历关联性容器,auto自动推导出的类型是容器中的value_type,相当于一个对组(std::pair)对象,提取键值对的方式如下:
it.first
it.second

通过对基于范围的for循环语法的介绍可以得知,在for循环内部声明一个变量的引用就可以修改遍历的表达式中的元素的值,但是这并不适用于所有的情况,对应set容器来说,内部元素都是只读的,这是由容器的特性决定的,因此在for循环中auto&会被视为const auto &

#include <iostream>
#include <set>
using namespace std;int main(void)
{set<int> st{ 1,2,3,4,5,6 };for (auto &item : st) {cout << item++ << endl;		// error, 不能给常量赋值}return 0;
}

除此之外,在遍历关联型容器时也会出现同样的问题,基于范围的for循环中,虽然可以得到一个std::pair引用,但是我们是不能修改里边的first值的,也就是key值。

#include <iostream>
#include <string>
#include <map>
using namespace std;int main(void)
{map<int, string> m{{1, "lucy"},{2, "lily"},{3, "tom"}};for (auto& item : m){// item.first 是一个常量cout << "id: " << item.first++ << ", name: " << item.second << endl;  // error}return 0;
}

基于范围的for循环遍历的对象可以是一个表达式或者容器/数组等。假设我们对一个容器进行遍历,在遍历过程中for循环对这个容器的访问频率是一次还是多次呢?我们通过下面的例子验证一下:

#include <iostream>
#include <vector>
using namespace std;vector<int> v{ 1,2,3,4,5,6 };
vector<int>& getRange()
{cout << "get vector range..." << endl;return v;
}int main(void)
{for (auto val : getRange()){cout << val << " ";}cout << endl;return 0;
}
get vector range...
1 2 3 4 5 6

从上面的结果中可以看到,不论基于范围的for循环迭代了多少次,函数getRange()只在第一次迭代之前被调用,得到这个容器对象之后就不会再去重新获取这个对象了。

结论:对应基于范围的for循环来说,冒号后边的表达式只会被执行一次。在得到遍历对象之后会先确定好迭代的范围,基于这个范围直接进行遍历如果是普通的for循环,在每次迭代的时候都需要判断是否已经到了结束边界

http://www.lryc.cn/news/338150.html

相关文章:

  • 引领智能互联时代,紫光展锐赋能百业创新发展
  • lv_micropython to download and building
  • 二叉树练习day.9
  • 2024年第十七届“认证杯”数学中国数学建模网络挑战赛B题思路
  • 【vue】slot 匿名插槽 / 具名插槽
  • FFmpeg: 自实现ijkplayer播放器-02环境搭建
  • Redis从入门到精通(十七)多级缓存(二)Lua语言入门、OpenResty集群的安装与使用
  • pytest常用钩子函数
  • .Net <% %>
  • 【C语言__编译和链接__复习篇2】
  • Jmeter —— 自动录制脚本
  • 使用python互相转换AVI、MP4、GIF格式视频文件
  • 11 Php学习:函数
  • 查询电脑用户名和组信息
  • 【Godot4.2】CanvasItem绘图函数全解析 - 9.绘制表格
  • 部署HDFS集群(完全分布式模式、hadoop用户控制集群、hadoop-3.3.4+安装包)
  • TCP协议简单总结
  • 【Qt 实现录音】
  • python:算法竞赛入门之一
  • 【大数据与云计算】虚拟机安装Linux
  • 从零开始编写一个cmake构建脚本
  • pringboot2集成swagger2出现guava的FluentIterable方法不存在
  • 进程线程的关系
  • 一些 VLP 下游任务的相关探索
  • 【opencv】示例-pca.cpp PCA图像重建演示
  • C语言中的编译和链接
  • 如何将三方库集成到hap包中——通过IDE集成cmak构建方式的C/C++三方库
  • Towards Street-Level Client-Independent IP Geolocation(2011年)(第二部分)
  • 软件测试过程和测试生命周期
  • python-study-day1