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

range for

1. 基于范围的for循环语法

C++11标准引入了基于范围的for循环特性,该特性隐藏了迭代器
的初始化和更新过程,让程序员只需要关心遍历对象本身,其语法也
比传统for循环简洁很多:

for ( range_declaration : range_expression )
{loop_statement;
}

基于范围的for循环不需要初始化语句、条件表达式以及更新表
达式,取而代之的是一个范围声明和一个范围表达式。其中范围声明
是一个变量的声明,其类型是范围表达式中元素的类型或者元素类型
的引用。而范围表达式可以是数组或对象,对象必须满足以下2个条件
中的任意一个。
    1.对象类型定义了begin和end成员函数。

    2. 定义了以对象参数为类型的begin和end普通函数。

#include <iostream>
#include <string>
#include <map>
std::map<int, std::string> index_map{ {1, "hello"}, {2, "world"},
{3, "!"} };
int int_array[] = { 0, 1, 2, 3, 4, 5 };
int main()
{for (const auto &e : index_map){std::cout << "key=" << e.first << ", value=" << e.second <<std::endl;}for (auto e : int_array){std::cout << e << std::endl;}
}

如果不会在循环过程中修改引用对象,那么推荐在范围声明中加上const限定符以帮助编译器生成更加高效的代码:
 

#include <iostream>
#include <vector>struct X
{X() { std::cout << "default ctor" << std::endl; }X(const X& other){std::cout << "copy ctor" << std::endl;}
};int main()
{std::vector<X> x(10);std::cout << "for (auto n : x)" << std::endl;for (auto n : x){}std::cout << "for (const auto &n : x)" << std::endl;for (const auto &n : x){}
}

2. 实现自己的range-for

//since C++17, until C++20for (range-declaration : range-expression )
{loop-statement
}The above syntax produces code equivalent to the following except for the lifetime expansion of temporaries of range-expression.
{auto && __range = range-expression ;  //gcc//auto &_range = range-expression;   //clangauto __begin = begin-expr ;auto __end = end-expr ;for ( ; __begin != __end; ++__begin){range-declaration = *__begin;loop-statement}
}

其中operator *用于编译器生成解引用代码,operator !=用于生成循环条件代码,而前缀版本的operator ++用于更新迭代器。

#include <iostream>template<typename T, size_t L>
class Iter
{
public:Iter(T* data, size_t idx):data{data}, idx{idx}{}T& operator*(){return *(data + idx);}Iter& operator++()//前缀operator ++{idx++;return *this;}bool operator != (const Iter& other){return other.idx != this->idx;}private:size_t idx{0};T* data;
};template<typename T, size_t L>
class array
{
public:using iterator = Iter<T, L>;array(std::initializer_list<T> list){T * ptr = data;for (const auto e : list){*ptr = e;ptr++;}}iterator begin(){return iterator(data, 0);}iterator end(){return iterator(data, L);}
private:T data[L]{};
};int main(void)
{array<int, 4> arr{10,20,30,40};for(auto iter = arr.begin(); iter != arr.end(); ++iter){std::cout << *iter << ' ';}for (const auto i : arr){std::cout << i << ' ';}std::cout << std::endl;
}

以上代码,类模板array中实现了iterator begin()和 iterator end(),在迭代器类Iter中实现了operator*(), operator++()[注意这里是前缀operator++],以及operator!=。

3. 临时范围表达式的陷阱

无论是C++11还是C++17,基于范围的for循环伪代码

//since C++17, until C++20

for (range-declaration : range-expression )

{

    loop-statement

}

The above syntax produces code equivalent to the following except for the lifetime expansion of temporaries of range-expression.

{

    auto && __range = range-expression ;  //gcc版本

    //auto &_range = range-expression;   //clang版本

    auto __begin = begin-expr ;

    auto __end = end-expr ;

    for ( ; __begin != __end; ++__begin)

    {

          range-declaration = *__begin;

          loop-statement

    }

}

对于下面这个例子代码

#include <iostream>
#include <vector>class RangeFor
{
public:std::vector<int>& items() { return data; }
private:std::vector<int> data{10, 20, 30, 40};
};RangeFor foo()
{RangeFor data;return data;
}int main(void)
{/*step 1: foo() return rvalue;step 2: items() return l_val_ref of rvalue.step 3: __range is an align for l_val_ref.step 4: rvalue(temp value) destory.now __range reference to dangling refenence.*/for (const auto& x : foo().items()) //gcc: unexcepted number//clang: segmentation fault{std::cout << x << " ";}return 0;
}

我们从C++ Insights分析看:

#include <iostream>
#include <vector>class RangeFor
{public: inline std::vector<int, std::allocator<int> > & items(){return this->data;}private: std::vector<int, std::allocator<int> > data;public: // inline RangeFor(RangeFor &&) noexcept = default;// inline ~RangeFor() noexcept = default;// inline constexpr RangeFor() noexcept(false) = default;
};RangeFor foo()
{RangeFor data = RangeFor() /* NRVO variable */;return data;
}int main()
{{std::vector<int, std::allocator<int> > & __range1 = foo().items();__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > __begin1 = __range1.begin();__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > __end1 = __range1.end();for(; __gnu_cxx::operator!=(__begin1, __end1); __begin1.operator++()) {int const & x = __begin1.operator*();std::operator<<(std::cout.operator<<(x), " ");}}return 0;
}

从clang角度编译器看: std::vector<int, std::allocator<int> > & __range1 = foo().items();

foo()返回临时右值,这个临时右值调用items返回了一个左值引用,然后 __range有引用了这个临时右值接口items返回的左值引用;这个句子结束后,这个临时右值销毁了,这个时候__range其实引用的是一个已经不存在对象的数据。

auto && __range = range-expression ;  

这个是是cpp reference上说的(应该是gcc版本的)。其最终结果也是一样的;我们来分析下上面那段代码:

/*
    step 1: foo() return temp rvalue;
    step 2: items() return l_val_ref of rvalue.
    step 3: __range is an alias for l_val_ref.
    step 4: rvalue(temp value) destory.
    now __range reference to dangling refenence.
*/
    for (const auto& x : foo().items()) //gcc: unexcepted number
                                                         //clang: segmentation fault
    {
        std::cout << x << " ";
    }

可能这里有些人觉得 定义了const auto&x可以捕获临时变量,这个是没错的;对于这个range-for来回代码相当于如下:

#include <iostream>
#include <vector>#include <string>class RangeFor
{
public:RangeFor() : data("rangeFor") { std::cout << "ctor" << std::endl; }std::string& getItems(){std::cout << "get data" << std::endl;return data;}~RangeFor(){std::cout << "dtor" << std::endl;data = "";}private:std::string data;
};RangeFor foo()
{RangeFor data;return data;
}int main(void)
{/*step 1: foo() return rvalue;step 2: items() return l_val_ref of rvalue.step 3: vec is an align for l_val_ref.step 4: rvalue(temp value) destory.*/const auto& ret = foo().getItems();//note: now vec reference to dangling refenence.if (ret.empty()){std::cout << "empty string" << std::endl;}else{std::cout << ret << std::endl;}return 0;
}

const auto& ret = foo(), getItems();这个 const左值引用ret作用于getItems返回的左值引用,这个getItems返回的左值引用(打比喻是毛)的真真数据是foo()返回的临时变量(打比喻是皮),这行代码结束后,临时变量被销毁了。getItems返回的左值引用,用一句话总结就是:皮之不存毛将焉附!

4. 基于范围的for 循环的初始化

C++17 引入了if 和switch 控制结构的可选初始化,C++20 现在为基于范围的for 循环引入了这
样一个可选的初始化。

for ( init-statement(optional); range-declaration : range-expression )

{

      loop-statement

}

The above syntax produces code equivalent to the following:

{

       init-statement

      auto && __range = range-expression ;

      auto __begin = begin-expr ;

      auto __end = end-expr ;

      for ( ; __begin != __end; ++__begin)

      {

            range-declaration = *__begin;

            loop-statement

       }

}

我们来看个代码示例:

#include <iostream>
#include <vector>class RangeFor
{
public:std::vector<int>& items() { return data; }
private:std::vector<int> data{10, 20, 30, 40};
};RangeFor foo()
{RangeFor data;return data;
}int main(void)
{//const auto& thing = foo();//for (const auto& x : thing.items())// C++17for (auto thing = foo(); const auto& x :thing.items()) //since c++20{std::cout << x << " ";}return 0;
}

相关文章:

range for

1. 基于范围的for循环语法 C11标准引入了基于范围的for循环特性&#xff0c;该特性隐藏了迭代器 的初始化和更新过程&#xff0c;让程序员只需要关心遍历对象本身&#xff0c;其语法也 比传统for循环简洁很多&#xff1a; for ( range_declaration : range_expression ) {loo…...

leetcode230 二叉搜索树中第K小的元素

题目 给定一个二叉搜索树的根节点 root &#xff0c;和一个整数 k &#xff0c;请你设计一个算法查找其中第 k 个最小元素&#xff08;从 1 开始计数&#xff09;。 示例 输入&#xff1a;root [5,3,6,2,4,null,null,1], k 3 输出&#xff1a;3 解析 这道题应该是能做出…...

.Net Core学习笔记 框架特性(注入、配置)

注&#xff1a;直接学习的.Net Core 6&#xff0c;此版本有没有startup.cs相关的内容 项目Program.cs文件中 是定义项目加载 启动的地方 //通过builder对项目进行配置、服务的加载 var builder WebApplication.CreateBuilder(args); builder.Services.AddControllers();//将…...

利用AI技术做电商网赚,这些百万级赛道流量,你还不知道?!

大家好&#xff0c;我是向阳 AI技术的飞速扩展已经势不可挡&#xff0c;不管你承不承认&#xff0c;AI 已经毫无争议的在互联网中占有一席之地了 无论你是做内容产业的&#xff0c;还是做电商的&#xff0c;你现在都躲不开 AI。 现在互联网行业的竞争就是这么残酷 互联网行业…...

leetcode-560 和为k的数组

一、题目描述 给你一个整数数组 nums 和一个整数 k &#xff0c;请你统计并返回 该数组中和为 k 的子数组的个数 。 子数组是数组中元素的连续非空序列。 注意&#xff1a;nums中的元素可为负数 输入&#xff1a;nums [1,1,1], k 2 输出&#xff1a;2输入&#xff1a;num…...

Spring Boot实战指南:从入门到企业级应用构建

目录 一、引言 二、快速入门 1. 使用Spring Initializr创建项目 三、Spring Boot基础概念与自动配置 1. 理解SpringBootApplication注解 2. 自动配置原理 3. 查看自动配置报告 四、Spring Boot核心特性及实战 1. 外部化配置 2. Actuator端点 3. 集成第三方库 五、Sp…...

OneAPI接入本地大模型+FastGPT调用本地大模型

将Ollama下载的本地大模型配置到OneAPI中&#xff0c;并通过FastGPT调用本地大模型完成对话。 OneAPI配置 新建令牌 新建渠道 FastGPT配置 配置docker-compose 配置令牌和OneAPI部署地址 配置config.json 配置调用的渠道名称和大模型名称 {"systemEnv": {&qu…...

Training-Free Consistent Text-to-Image Generation # 论文阅读

URL https://arxiv.org/pdf/2402.03286 TL;DR 2024 年 2 月 nvidia 的文章。提出了一种不需要任何额外训练的主体保持方法&#xff0c;可以一次生成的 batch 中&#xff0c;通过多个 prompt 生成对应的多张图片&#xff0c;这些图片都可以拥有一个主体。 本文提出的方法通过…...

Spring 中常用的手动装载 bean 方法

在 Spring 的 bean 装载条件中&#xff0c;虽然 Spring 给我们提供了非常好用便捷的 Condition 相关注解&#xff0c;但是很多时候 Condition 相关注解并不满足我们的需求&#xff0c;我需要更复杂的条件手动控制是否装置 bean。这个时候我们就可以实现 Spring 为我们提供的几个…...

如何合理设置Java线程池大小

如何合理设置Java线程池大小&#xff1a;依据任务类型定制策略 Java线程池的合理配置直接关系到系统性能和资源利用率。根据任务性质的不同&#xff0c;合理的线程池大小设置策略也有所区别&#xff0c;主要包括CPU密集型、IO密集型及混合型任务。 1. CPU密集型任务 特点&am…...

python3 pandas

pandas - Python Data Analysis Library...

【B站 heima】小兔鲜Vue3 项目学习笔记Day02

文章目录 Pinia1.使用2. pinia-计数器案例3. getters实现4. 异步action5. storeToRefsx 数据解构保持响应式6. pinia 调试 项目起步1.项目初始化和git管理2. 使用ElementPlus3. ElementPlus 主题色定制4. axios 基础配置5. 路由设计6. 静态资源初始化和 Error lens安装7.scss自…...

RedisTemplate 实现基于 Value 操作的简易锁机制

在高并发场景下&#xff0c;确保操作的原子性和避免竞态条件至关重要。Redis 提供了丰富的数据结构和操作&#xff0c;是实现分布式锁的一个高效选择。本文将介绍如何使用 RedisTemplate 的 opsForValue().setIfAbsent() 方法来实现一种简单的锁机制&#xff0c;并提供一个示例…...

其它高阶数据结构⑦_Skiplist跳表_概念+实现+对比

目录 1. Skiplist跳表的概念 2. Skiplist跳表的效率 3. Skiplist跳表的实现 3.1 力扣1206. 设计跳表 3.2 Skiplist的初始化和查找 3.3 Skiplist的增加和删除 3.4 Skiplist的源码和OJ测试 4. 跳表和平衡搜索树/哈希表的对比 本篇完。 1. Skiplist跳表的概念 skiplist是…...

力扣230. 二叉搜索树中第K小的元素

Problem: 230. 二叉搜索树中第K小的元素 文章目录 题目描述思路复杂度Code 题目描述 思路 直接利用二叉搜索树中序遍历为一个有序序列的特性&#xff1a; 记录一个int变量rank&#xff0c;在中序遍历时若当前rank k则返回当前节点值 复杂度 时间复杂度: O ( n ) O(n) O(n);其…...

Linux_应用篇(07) 系统信息与系统资源

在应用程序当中&#xff0c;有时往往需要去获取到一些系统相关的信息&#xff0c;譬如时间、日期、以及其它一些系统相关信息&#xff0c;本章将向大家介绍如何通过 Linux 系统调用或 C 库函数获取系统信息&#xff0c; 譬如获取系统时间、日期以及设置系统时间、日期等&#x…...

基于Vue的验证码实现

一、验证码核心实现 创建slide-verify.vue&#xff0c;代码如下&#xff1a; <template><divclass"slide-verify":style"{ width: w px }"id"slideVerify"onselectstart"return false;"><!-- 图片加载遮蔽罩 -->&…...

P4【力扣217,389,496】【数据结构】【哈希表】C++版

【217】存在重复元素 给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 &#xff0c;返回 true &#xff1b;如果数组中每个元素互不相同&#xff0c;返回 false 。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3,1] 输出&#xff1a;true 示例 2&#xff1a;…...

PE文件(六)新增节-添加代码作业

一.手动新增节添加代码 1.当预备条件都满足&#xff0c;节表结尾没有相关数据时&#xff1a; 现在我们将ipmsg.exe用winhex打开&#xff0c;在节的最后新增一个节用于存放我们要增加的数据 注意&#xff1a;飞鸽的文件对齐和内存对齐是一致的 先判断节表末尾到第一个节之间…...

ICRA 2024: NVIDIA 联合多伦多大学、加州大学伯克利分校、苏黎世联邦理工学院等研究人员开发了精细操作的手术机器人

英伟达&#xff08;NVIDIA&#xff09;正与学术研究人员合作&#xff0c;研究手术机器人。 NVIDIA 联合多伦多大学、加州大学伯克利分校、苏黎世联邦理工学院和佐治亚理工学院的研究人员开发了 ORBIT-Surgical&#xff0c;一个训练机器人的模拟框架&#xff0c;可以提高手术团…...

css实现圆环展示百分比,根据值动态展示所占比例

代码如下 <view class""><view class"circle-chart"><view v-if"!!num" class"pie-item" :style"{background: conic-gradient(var(--one-color) 0%,#E9E6F1 ${num}%),}"></view><view v-else …...

IGP(Interior Gateway Protocol,内部网关协议)

IGP&#xff08;Interior Gateway Protocol&#xff0c;内部网关协议&#xff09; 是一种用于在一个自治系统&#xff08;AS&#xff09;内部传递路由信息的路由协议&#xff0c;主要用于在一个组织或机构的内部网络中决定数据包的最佳路径。与用于自治系统之间通信的 EGP&…...

Java-41 深入浅出 Spring - 声明式事务的支持 事务配置 XML模式 XML+注解模式

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

学习STC51单片机31(芯片为STC89C52RCRC)OLED显示屏1

每日一言 生活的美好&#xff0c;总是藏在那些你咬牙坚持的日子里。 硬件&#xff1a;OLED 以后要用到OLED的时候找到这个文件 OLED的设备地址 SSD1306"SSD" 是品牌缩写&#xff0c;"1306" 是产品编号。 驱动 OLED 屏幕的 IIC 总线数据传输格式 示意图 …...

VTK如何让部分单位不可见

最近遇到一个需求&#xff0c;需要让一个vtkDataSet中的部分单元不可见&#xff0c;查阅了一些资料大概有以下几种方式 1.通过颜色映射表来进行&#xff0c;是最正规的做法 vtkNew<vtkLookupTable> lut; //值为0不显示&#xff0c;主要是最后一个参数&#xff0c;透明度…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个生活电费的缴纳和查询小程序

一、项目初始化与配置 1. 创建项目 ohpm init harmony/utility-payment-app 2. 配置权限 // module.json5 {"requestPermissions": [{"name": "ohos.permission.INTERNET"},{"name": "ohos.permission.GET_NETWORK_INFO"…...

AspectJ 在 Android 中的完整使用指南

一、环境配置&#xff08;Gradle 7.0 适配&#xff09; 1. 项目级 build.gradle // 注意&#xff1a;沪江插件已停更&#xff0c;推荐官方兼容方案 buildscript {dependencies {classpath org.aspectj:aspectjtools:1.9.9.1 // AspectJ 工具} } 2. 模块级 build.gradle plu…...

Mobile ALOHA全身模仿学习

一、题目 Mobile ALOHA&#xff1a;通过低成本全身远程操作学习双手移动操作 传统模仿学习&#xff08;Imitation Learning&#xff09;缺点&#xff1a;聚焦与桌面操作&#xff0c;缺乏通用任务所需的移动性和灵活性 本论文优点&#xff1a;&#xff08;1&#xff09;在ALOHA…...

基于 TAPD 进行项目管理

起因 自己写了个小工具&#xff0c;仓库用的Github。之前在用markdown进行需求管理&#xff0c;现在随着功能的增加&#xff0c;感觉有点难以管理了&#xff0c;所以用TAPD这个工具进行需求、Bug管理。 操作流程 注册 TAPD&#xff0c;需要提供一个企业名新建一个项目&#…...

【Redis】笔记|第8节|大厂高并发缓存架构实战与优化

缓存架构 代码结构 代码详情 功能点&#xff1a; 多级缓存&#xff0c;先查本地缓存&#xff0c;再查Redis&#xff0c;最后才查数据库热点数据重建逻辑使用分布式锁&#xff0c;二次查询更新缓存采用读写锁提升性能采用Redis的发布订阅机制通知所有实例更新本地缓存适用读多…...