I tried to use lambda function with sort
, but was getting "Segmentation fault" errors. I managed to simplify the code to the following:
#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<<")
";
return (a<b?-1:(a>b?1:0));
});
return 0;
}
I compile and run this code in Ubuntu 11.04 (x64) using
g++ -std=gnu++0x test2.cpp && ./a.out
.
It prints a lot of pairs of the form (large_integer, 1008), a couple of (0, 1008) and exits with "Segmentation fault".
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…