What does the auto c++ keyword do? [duplicate]

不打扰是莪最后的温柔 提交于 2019-12-04 04:00:02

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!