问题
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?
回答1:
Compiler guess maxIterator
's type. If spec
's type is float []
, maxIterator
type is float *
.
回答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
来源:https://stackoverflow.com/questions/21226402/what-does-the-auto-c-keyword-do