Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a warning for this range-based for loop in C++?

I am currently self-teaching myself C++ using Bjarne Stroustrup's book (2nd ed). In one of the examples, he uses a range-for-loop to read the elements in a vector. When I wrote and compiled the code for myself I get this warning. When I run the code, it seems to be working and calculates the mean. Why am I receiving this warning and should I ignore it? Also, why is the range-for using int instead of double in the example, but still returns a double?

temp_vector.cpp:17:13: warning: range-based for loop is a C++11 
extension [-Wc++11-extensions]

This is the code

#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 : temps)     
    sum += x;

  //compute and print the mean of the elements in the vector
      cout << "Mean temperature: " << sum / temps.size() << endl;

  return 0;
}

On a similar note: how should I view the range-for in terms of a standard for loop?

like image 531
Efraín González Avatar asked Mar 14 '18 04:03

Efraín González


People also ask

Is there range-based for loop in C?

Range-based for loop (since C++11) Executes a for loop over a range. Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container.

Why is auto used in for loop?

Range-based for loop in C++ Often the auto keyword is used to automatically identify the type of elements in range-expression. range-expression − any expression used to represent a sequence of elements. Also Sequence of elements in braces can be used.

Are range-based for loops faster?

Range-for is as fast as possible since it caches the end iterator[citationprovided], uses pre-increment and only dereferences the iterator once. Then, yes, range-for may be slightly faster, since it's also easier to write there's no reason not to use it (when appropriate).

Are not allowed in C ++ 98 mode?

range-based 'for' loops are not allowed in C++98 mode You either have to enable C++11 support in your compiler (e.g. with -std=c++11 for gcc 4.8. x), use a newer, directly c++11 compatible compiler, or compile USER-COLVARS without the Lepton library.


2 Answers

Since nobody is showing how to use C++ 11 with g++, it looks like this...

g++ -std=c++11 your_file.cpp -o your_program

Hopefully this saves Google visitors an extra search.

like image 176
yanksrock1000 Avatar answered Sep 28 '22 11:09

yanksrock1000


For those who use code runner extension on VS code, go to code runner extension settings. find -> Code-runner:Executer map. Click on edit in settings.json and find cpp script for terminal and set it like follows:

"cpp": "cd $dir && g++  -std=c++11 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
like image 29
ʝƁ Ʈɦɘ Pɑɨŋ Kɨɭɭeɽ Avatar answered Sep 28 '22 11:09

ʝƁ Ʈɦɘ Pɑɨŋ Kɨɭɭeɽ