【问题标题】:std::greater not defined in MSVC2012MSVC2012 中未定义 std::greater
【发布时间】:2013-05-10 04:21:13
【问题描述】:

为什么 std::greater 不再是 Visual Studio 2012 中 std 命名空间的一部分?我现在需要包含<functional>

我认为 STL 库在不同的工具集中保持不变

【问题讨论】:

  • 首先应该是<functional>。其次,您总是需要包含它才能访问std::greater
  • 不是<functional.h>,而是<functional>

标签: c++ visual-studio-2010 visual-c++ visual-studio-2012 stl


【解决方案1】:

为什么 std::greater 不再是 Visual Studio 2012 中 std 命名空间的一部分?

如果是这样的话,我会感到非常惊讶。

我现在需要包含<functional.h>

不,您需要包含<functional>,因为它是定义所有标准函数对象的标头,包括greater

我认为 STL 库在不同的工具集中保持不变

他们应该是。但是标题可以包含其他标题,因此有时即使您没有包含正确的标题,您也会发现某些内容可用。但是你不能依赖它,就像你在这里找到的那​​样。始终包含您使用的东西所需的所有标题。

【讨论】:

    【解决方案2】:

    以下示例取自 here,在 Visual Studio 2012 中为我正确编译和执行:

    // greater example
    #include <iostream>     // std::cout
    #include <functional>   // std::greater
    #include <algorithm>    // std::sort
    
    int main () {
      int numbers[]={20,40,50,10,30};
      std::sort (numbers, numbers+5, std::greater<int>());
      for (int i=0; i<5; i++)
        std::cout << numbers[i] << ' ';
      std::cout << '\n';
      return 0;
    }
    

    输出:

    50 40 30 20 10

    根据问题 cmets 和上面的示例,您需要包含 &lt;functional&gt;,而不是 &lt;functional.h&gt;

    【讨论】:

      猜你喜欢
      • 2021-10-29
      • 1970-01-01
      • 2019-02-11
      • 1970-01-01
      • 2018-09-18
      • 2013-01-27
      • 2012-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多