4

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?

user3213163
  • 607
  • 3
  • 8
  • 10

2 Answers2

2

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

ikh
  • 10,119
  • 1
  • 31
  • 70
2

In C++11, the keyword auto deduces the type of declared variable from its initialization expression. Hence, in your code it deduces type of maxIterator.

For more information on auto look here

yuvi
  • 1,032
  • 1
  • 12
  • 22