What does the auto c++ keyword do?

user3213163 picture user3213163 · Jan 20, 2014 · Viewed 7.7k times · Source

I recently came accross the keyword auto in c++.

In the code:

auto maxIterator = std::max_element(&spec[0], &spec[sampleSize]);
float maxVol = *maxIterator;

// Normalize
if (maxVol != 0)
  std::transform(&spec[0], &spec[sampleSize], &spec[0], [maxVol] (float dB) -> float { return dB / maxVol; });

This is to do with running a frequency analysis on a audio stream. From the website: http://katyscode.wordpress.com/2013/01/16/cutting-your-teeth-on-fmod-part-4-frequency-analysis-graphic-equalizer-beat-detection-and-bpm-estimation/

I have searched the forums but It says that there is no use for the keyword. Could someone please explain the use of it here.

I am quite new to c++ so please try not to make the answers too complicated. Thanks so much all.

Did auto make maxIterator a pointer also?

Answer

ikh picture ikh · Jan 20, 2014

Compiler guess maxIterator's type. If spec's type is float [], maxIterator type is float *.