基于范围的for循环

[复制链接]
发表于 2026-3-10 19:00:00 | 显示全部楼层 |阅读模式

c++11基于范围的for循环,语法:

for (Type declaration : expression)
{
    // 循环体
}

在上面的语法格式中Type declaration表示遍历声明,在遍历过程中,当前被遍历导的元素会被存储到声明的变量declaration中。expression是要遍历的对象,它可以是表达式、容器、数组、初始化列表等。

如下代码:

#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;

    for(int value : t){           //第二次遍历
        cout << value << " ";
    }
    
    cout << endl;
    
    for(auto& value : t){        //第三次遍历
        cout << value++ << " ";
    }
    
    cout << endl;
    
    for(auto& value : t){          //第四次遍历
        cout << value << " ";
    }
    
    cout << endl;
    
    for(const auto& value : t){      //第五次遍历
        cout << value << " ";
    }
    
    return 0;
}

运行结果:

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
2 3 4 5 6 7
2 3 4 5 6 7

在上面的例子中,第一次遍历是将容器中的元素拷贝到声明的遍历变量value中, 因此无法对value的++操作是不影响原数据的,所以第二次遍历的结果不会有改变。第二次遍历中声明遍历变量用的int,因为这里知道遍历的t容器中全是int类型,auto推导出的就是int类型。

第三次遍历时,遍历变量value声明成引用类型,不仅没有拷贝的过程使得效率更高,而且对value的++操作会直接作用到原数据上,因此第四次遍历的结果会全部+1。

在第五次遍历时,遍历变量声明称const auto&,value被限制成只读权限,如果对value进行++操作会报错。

使用注意

1. 关系型容器

在使用基于范围的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;
}

上述代码使用了基于范围的for循环方式和普通的for循环方式两种方式对map进行遍历,注意到:

  • 使用普通for循环遍历关系型容器时,auto自动推导出的是一个迭代器类型,需要使用迭代器的类型方式取出元素中的键值对,迭代器返回的是地址:

    it->first;

    it->second;

  • 使用基于范围的for循环方式遍历关系型容器时,auto自动推导出的类型是容器中的value_type,相当于一个std::pair对象,提取键值对的方式:

    it.first;

    it.second;

2. 元素只读

在对基于范围的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;
}

除此之外,在遍历关系型容器map时也会出现同样的问题,基于范围的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循环遍历的对象可以是一个表达式或者容器/数组等。在我们对一个容器进行遍历过程中对这个容器的访问次数时一次还是多次呢?

#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;
}

上面代码通过getRange()函数对容器v进行访问,每访问一次就会输出一次get vector range... 。

输出结果:

get vector range...
1 2 3 4 5 6

可以从上面的结果看出只访问了一次,所以不管遍历的容器里面有多少个元素,都只在第一次迭代之前被访问一次,得到这个容器对象之后不会再去重新获取这个对象了。

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



来源:https://www.cnblogs.com/ggkx/p/19697670
回复

使用道具 举报

发表于 2026-9-28 19:47:34 | 显示全部楼层
哇楼主这个基础讲解很清晰,刚好给新手入门范围for特别合适~ 补充两个日常用的小注意点吧:
  1. // 如果要修改容器内的元素一定要加引用,不然修改的只是临时副本,原容器数据不会变
  2. for (auto& value : t) {
  3.     value++; // 这样才能真的修改到t里存储的元素
  4. }
  5. // 只读遍历的话推荐加const引用,避免大元素拷贝浪费性能
  6. for (const auto& value : t) {
  7.     cout << value << " ";
  8. }
复制代码

对了咱们源码公社还有大佬整理的全套C++11/14/17/20新特性的代码示例包,里面还有好多范围for结合结构化绑定之类的进阶用法,需要的小伙伴可以去站内搜搜,找不到的话可以留评论我发你呀~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

圆梦公社,专注于为全球华人提供纯粹技术交流的地方,请勿发布任何政治及违法的言论。如有相关侵权、举报、投诉及建议等,请发 E-mail:dzh188@hotmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部