相关疑难解决方法(0)

std :: sort应该在c ++ 0x/c ++ 11中使用lambda函数吗?

我尝试使用lambda函数sort,但是出现了"Segmentation fault"错误.我设法将代码简化为以下内容:

#include <iostream>
#include <algorithm>

int main()
{
  const int len = 18;
  int intArr[len];
  for (int i=0;i<len;i++) intArr[i]=1000+i;
  // The following is expected to sort all but the last element of the array
  std::sort(intArr, intArr + len -1, [](int a, int b)
    {
      std::cout<<"("<<a<<", "<<b<<")\n";
      return (a<b?-1:(a>b?1:0));
    });
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我使用在Ubuntu 11.04(x64)中编译并运行此代码

g++ -std=gnu++0x test2.cpp && ./a.out.

它会打印很多对形式(large_integer,1008),一对(0,1008)和退出"Segmentation fault".

c++ debugging c++11

30
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

debugging ×1