C++11中新增的for功能

一、引言

        C++11中新增的for循环功能,通常被称为范围基于的for循环(Range-based for loop),它是一种简化的for循环语法,用于遍历容器(如数组、std::vector、std::list等)或其他序列中的所有元素。这种循环使得代码更加简洁、易读,并且减少了编写迭代代码时出错的机会。

二、基本语法

for (datatype var: collection){
    // 循环代码
}

语法说明:
        datatype:表示某一数据类型,常使用auto自动推断。也可以使用类型的引用;

        var:变量名

        collection:可以迭代的集合类型,如数组,及STL中的大部分容器(stack和queue不可以)。

三、示例

1. 遍历数组

#include <iostream>  
  
int main() {  
    int arr[] = {1, 2, 3, 4, 5};  
    for (auto elem : arr) {  
        std::cout << elem << ' ';  
    }  
    //输出1 2 3 4 5
    return 0;  
}

2. 遍历vector

#include <iostream>  
#include <vector>  
  
int main() {  
    std::vector<int> vec = {1, 2, 3, 4, 5};  
    for (auto elem : vec) {  
        std::cout << elem << ' ';  
    }  
    return 0;  
}

3. 遍历string

#include <iostream>  
#include <string>  
  
int main() {  
    std::string str = "Hello, World!";  
    for (auto ch : str) {  
        std::cout << ch;  
    }  
    return 0;  
}

4. 遍历map

#include <iostream>
#include <iomanip>
#include <map>
#include <unordered_map> 

int main(){
	using namespace std;
	const int WIDTH = 15;
		
	map<int, string> mp{
		make_pair<int, string>(1, "one"),
		make_pair<int, string>(2, "two"),
		make_pair<int, string>(3, "three")
	};
	cout << setw(WIDTH) << right << "map: ";
	for (const auto &item: mp){
		cout << item.first << ":" << item.second << "  ";
	}
	cout << endl;
	
	multimap<int, string> mmp{
		make_pair<int, string>(1, "one"),
		make_pair<int, string>(2, "two"),
		make_pair<int, string>(3, "three"),
		make_pair<int, string>(1, "one")
	};
	cout << setw(WIDTH) << right << "multimap: ";
	for (const auto &item: mmp){
		cout << item.first << ":" << item.second << "  ";
	}
	cout << endl;
	
	unordered_map<int, string> ump{
		make_pair<int, string>(1, "one"),
		make_pair<int, string>(2, "two"),
		make_pair<int, string>(3, "three")
	};
	cout << setw(WIDTH) << right << "un_map: ";
	for (const auto &item: ump){
		cout << item.first << ":" << item.second << "  ";
	}
	cout << endl;
	
	unordered_multimap<int, string> ummp{
		make_pair<int, string>(1, "one"),
		make_pair<int, string>(2, "two"),
		make_pair<int, string>(3, "three"),
		make_pair<int, string>(1, "one")
	};
	cout << setw(WIDTH) << right << "un_multimap: ";
	for (const auto &item: ummp){
		cout << item.first << ":" << item.second << "  ";
	}
	cout << endl;

    return 0;
}

四、注意事项

  1. 类型自动推导:通常使用auto关键字来自动推导循环变量的类型,这使得代码更加灵活和易于维护。
  2. 只读访问:在某些情况下,你可能需要遍历容器但不想修改其中的元素。虽然范围基于的for循环本身不直接提供只读语义,但你可以通过声明为const auto&来避免修改元素(这对于大型对象或容器尤其有用,因为它可以避免不必要的拷贝)。
  3. 迭代器失效:在遍历容器时,如果容器的大小或内容发生了变化(例如,通过添加或删除元素),可能会导致迭代器失效。虽然范围基于的for循环通常不会直接操作迭代器,但如果循环体内有操作可能改变容器大小或结构,那么需要小心处理。
  4. 与STL算法结合使用:虽然范围基于的for循环很方便,但在需要更复杂的迭代逻辑时(如过滤、转换等),使用STL算法(如std::for_each、std::copy_if等)可能更加灵活和强大。

五、小结

        总的来说,C++11中的范围基于的for循环是一个强大且方便的特性,它极大地简化了遍历容器和序列的代码编写。

附:c++11新增的其他性

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hn_tzy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值