I'm writing a c++ program to sort int pairs according to its first element.
int a[5000][2];
I call the sort function with this command:
sort(a, a + n, cmp);
where n is the number of elements I want to sort, and cmp is defined like this:
bool cmp(int *a, int *b) {
return a[0] < b[0];
}
I have already included <algorithm>. However, while compiling the file using g++, it failed with a large block of error info, all pointing to the lib file rather than my program. I'm sure the problem is with my calling sort function, because once I remove the sort function, the compilation succeeded.
Anyone knows what's wrong with my program?
Here's some of the error info:
In file included from /usr/include/c++/4.8/bits/stl_pair.h:59:0,
from /usr/include/c++/4.8/bits/stl_algobase.h:64,
from /usr/include/c++/4.8/bits/char_traits.h:39,
from /usr/include/c++/4.8/ios:40,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from milk2.cpp:7:
/usr/include/c++/4.8/bits/stl_algo.h: In instantiation of ‘void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = bool (*)(int*, int*)]’:
/usr/include/c++/4.8/bits/stl_algo.h:2226:70: required from ‘void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = int (*)[2]; _Compare = bool (*)(int*, int*)]’
/usr/include/c++/4.8/bits/stl_algo.h:5500:55: required from ‘void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = int (*)[2]; _Compare = bool (*)(int*, int*)]’
milk2.cpp:32:29: required from here
/usr/include/c++/4.8/bits/stl_algo.h:2162:11: error: array must be initialized with a brace-enclosed initializer
__val = _GLIBCXX_MOVE(*__i);
My program is milk2.cpp. The 7th line is:
#include <iostream>
and the 32nd line is the line calling sort function
milk2.cpp:32:29: required from hereLook at line 32.