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

基于范围的for循环

C++11基于范围的for循环,语法格式:

  • declaration 表示遍历声明,在遍历过程中当前被遍历到的元素会被存储到声明的变量中
  • expression 是要遍历的对象,它可以是 表达式、容器、数组、初始化列表等
for (declaration : expression)
{// 循环体
}
使用基于范围的for循环遍历容器
#include <iostream>
#include <vector>
using namespace std;int main() {vector<int> t{1, 2, 3, 4, 5};for(auto value : t){cout << value << " ";}cout << endl;return 0;
}

一、在容器遍历时修改值

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

#include <iostream>
#include <vector>
using namespace std;int main() {vector<int> t{1, 2, 3, 4, 5};cout << "遍历修改之前的元素"<< endl;for(auto value : t){cout << value << " ";}cout << endl;cout << "遍历修改之后的元素"<< endl;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() {vector<int> t{1, 2, 3, 4, 5};cout << "遍历修改之前的元素"<< endl;for (const auto& value : t){cout << value << " ";}return 0;
}

三、使用细节

1.关系型容器  k-v

#include <iostream>
#include <map>
#include <string>
using namespace std;int main (void){map <int,string> mymap{{1,"apple"},{2,"banana"},{3,"orange"}};//基于范围的for循环for (auto &item : mymap){cout<<"id:" << item.first << ",name" << item.second << endl;}//普通的for循环方式for (auto it = mymap.begin(); it!= mymap.end(); ++it){cout<<"id:" << it->first << ",name" << it->second << endl;}//不能修改 first的值   item.first是一个常量   item.second可以修改for(auto &item : mymap){cout<<"id:" << item.first << ",name" << item.second + "666" << endl;}return 0;}

 

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

  2. 使用基于范围的for循环遍历关联性容器,auto自动推导出的类型是容器中的value_type,相当于一个对组(std::pair)对象,提取键值对的方式如下:
    it.first
    it.second

2.元素只读

对于set容器来说,内部的元素都是只读的,这是由容器的特性决定的,因此在for循环中auto & 会被视为const auto &。

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

3.访问次数(对要遍历集合的访问次数)

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

 只访问一次

http://www.xdnf.cn/news/999181.html

相关文章:

  • 【力扣 简单 C】206. 反转链表
  • 基于深度学习的异常检测系统:原理、实现与应用
  • 手机射频功放测试学习(一)——手机线性功放的主要测试指标
  • 落水人员目标检测数据集(猫脸码客第253期)
  • 开源 内存分配器 mimalloc 在 windwos 的编译及使用
  • Unity json解析选择实测
  • xhtml2pdf中文屏幕溢出问题
  • SpringBoot后端开发知识点总结(持续更新)
  • 《棒球万事通》棒球战术介绍·棒球1号位
  • 在rust中执行命令行输出中文乱码解决办法
  • android:foregroundServiceType详解
  • 亚马逊云服务器(AWS)会限制用户使用吗?深度解读AWS资源政策
  • 用idea进行数据同步
  • List ToMap优化优化再优化到极致
  • 成功解决 ValueError: Unable to find resource t64.exe in package pip._vendor.distlib
  • 51la无法统计IP?悟空统计精准IP归属地
  • 模板字符串使用点击事件【VUE3】
  • 使用Ollama+open-webui搭建本地AI模型
  • 榕壹云信用租赁系统:免押金全品类租赁解决方案,区块链+多因子认证赋能
  • 财政部长斯科特·贝森特预测,美元支持的稳定币将达到 2 万亿美元
  • [C++11] : 谈谈包装器和lambda表达式,仿函数,bind的坑
  • 分布式MQTT客户端看门狗机制设计与实现
  • ShardingSphere解析:分布式数据库中间件的分片设计与事务管理实践
  • react实现axios 的简单封装
  • 单链表经典算法
  • 【鸿蒙开发】组件动态创建
  • Linux检验库是否安装成功
  • 多线程(4)
  • 【大模型】实践之1:macOS一键部署本地大模型
  • std::make_shared简化智能指针 `std::shared_ptr` 的创建过程,并提高性能(减少内存分配次数,提高缓存命中率)