【问题标题】:What does the auto c++ keyword do? [duplicate]auto c++ 关键字有什么作用? [复制]
【发布时间】:2014-01-20 04:45:52
【问题描述】:

我最近在 c++ 中遇到了关键字 auto。

在代码中:

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; });

这与对音频流运行频率分析有关。 来自网站:http://katyscode.wordpress.com/2013/01/16/cutting-your-teeth-on-fmod-part-4-frequency-analysis-graphic-equalizer-beat-detection-and-bpm-estimation/

我搜索了论坛,但它说关键字没有用。有人可以在这里解释一下它的用途吗?

我对 c++ 很陌生,所以请尽量不要让答案太复杂。 非常感谢。

auto 是否也使 maxIterator 成为指针?

【问题讨论】:

标签: c++ auto fmod


【解决方案1】:

编译器猜测maxIterator 的类型。如果spec的类型是float [],则maxIterator的类型是float *

【讨论】:

    【解决方案2】:

    在 C++11 中,关键字 auto 从其初始化表达式中推断出声明变量的类型。因此,在您的代码中,它推断出maxIterator 的类型。

    有关auto的更多信息,请查看here

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 2020-08-23
      • 2012-07-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      相关资源
      最近更新 更多