我目前正在使用Bjarne Stroustrup的书(第2版)自学C++.在其中一个示例中,他使用for-for-loop来读取向量中的元素.当我为自己编写和编译代码时,我得到了这个警告.当我运行代码时,它似乎正在工作并计算平均值.为什么我收到这个警告,我应该忽略它吗?另外,为什么范围 - 在示例中使用int而不是double,但仍返回double?
temp_vector.cpp:17:13: warning: range-based for loop is a C++11
extension [-Wc++11-extensions]
Run Code Online (Sandbox Code Playgroud)
这是代码
#include<iostream>
#include<vector>
using namespace std;
int main ()
{
vector<double> temps; //initialize a vector of type double
/*this for loop initializes a double type vairable and will read all
doubles until a non-numerical input is detected (cin>>temp)==false */
for(double temp; cin >> temp;)
temps.push_back(temp);
//compute sum of all objects in vector temps
double sum = 0;
//range-for-loop: for all ints in vector temps.
for(int x …Run Code Online (Sandbox Code Playgroud)