DevC++不能使用vector容器,同时也不支持范围for循环问题解决

4月的第一天,我在DevC++ 上使用vector 动态数组容器的时报错了

同时,也不能使用范围for语句 即:for(int a : arr){}

/*
错误原因:'vector' was not declared in this scope
修正方法:导入<vector>头文件即可
*/
/*
错误原因:range-based'for'loops are not allowed in C++ 98 mode
修改方式:见下图
*/
#include<iostream>
#include<vector>
using namespace std;

int main(){
	vector<int> nums(5,5);//第一个参数为容量,第二个参数为默认值 
	nums.push_back(6);//这里只能使用一些C++标准模板库中的函数
	for(int num : nums)
		cout << num << " "; 
	return 0;
}

第一步:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-G8JptdKS-1648813719693)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20220401105142915.png)]

第二步:在第二个框中加上" -std= c++11"

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uxiqGbjz-1648813719694)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20220401105359207.png)]

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐