给标准库函数传函数指针总是不成功,求给看看错在哪

aftermisak 2013-04-24 08:58:08
int bijiao1(int a,int b)
{
int c = a-b;
return c;
}
int a[10] ={1,2,3,0,9,8,7,6,5,4};
sort(a,a+10,bijiao1);

就是这样,执行到sort中一定会报错
...全文
159 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
青松2 2013-04-24
  • 打赏
  • 举报
回复
引用 3 楼 aftermisak 的回复:
引用 2 楼 lhfslhfs 的回复: bool bijiao1(int a,int b) { return a>b; } 谢谢 我试了,确实可以,我把更复杂的那个改成返回bool型的也通过了, 必须要传返回bool类型的么?原来那个出错的根本原因是什么呢。
Standard C++ Library Reference sort Example See Also Send Feedback Arranges the elements in a specified range into a nondescending order or according to an ordering criterion specified by a binary predicate. template<class RandomAccessIterator> void sort( RandomAccessIterator _First, RandomAccessIterator _Last ); template<class RandomAccessIterator, class Predicate> void sort( RandomAccessIterator _First, RandomAccessIterator _Last, Predicate _Comp ); Parameters _First A random-access iterator addressing the position of the first element in the range to be sorted. _Last A random-access iterator addressing the position one past the final element in the range to be sorted. _Comp User-defined predicate function object that defines the comparison criterion to be satisfied by successive elements in the ordering. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied. This comparator function must impose a strict weak ordering on pairs of elements from the sequence. For more information, see Algorithms. Remarks The range referenced must be valid; all pointers must be dereferenceable and within the sequence the last position is reachable from the first by incrementation. Elements are equivalent, but not necessarily equal, if neither is less than the other. The sort algorithm is not stable and so does not guarantee that the relative ordering of equivalent elements will be preserved. The algorithm stable_sort does preserve this original ordering. The average of a sort complexity is O(N log N), where N = _Last – _First. Example Copy Code // alg_sort.cpp // compile with: /EHsc #include <vector> #include <algorithm> #include <functional> // For greater<int>( ) #include <iostream> // Return whether first element is greater than the second bool UDgreater ( int elem1, int elem2 ) { return elem1 > elem2; } int main( ) { using namespace std; vector <int> v1; vector <int>::iterator Iter1; int i; for ( i = 0 ; i <= 5 ; i++ ) { v1.push_back( 2 * i ); } int ii; for ( ii = 0 ; ii <= 5 ; ii++ ) { v1.push_back( 2 * ii + 1 ); } cout << "Original vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; sort( v1.begin( ), v1.end( ) ); cout << "Sorted vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; // To sort in descending order. specify binary predicate sort( v1.begin( ), v1.end( ), greater<int>( ) ); cout << "Resorted (greater) vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; // A user-defined (UD) binary predicate can also be used sort( v1.begin( ), v1.end( ), UDgreater ); cout << "Resorted (UDgreater) vector v1 = ( " ; for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ ) cout << *Iter1 << " "; cout << ")" << endl; } Copy Code Original vector v1 = ( 0 2 4 6 8 10 1 3 5 7 9 11 ) Sorted vector v1 = ( 0 1 2 3 4 5 6 7 8 9 10 11 ) Resorted (greater) vector v1 = ( 11 10 9 8 7 6 5 4 3 2 1 0 ) Resorted (UDgreater) vector v1 = ( 11 10 9 8 7 6 5 4 3 2 1 0 ) Requirements Header: <algorithm> Namespace: std See Also
aftermisak 2013-04-24
  • 打赏
  • 举报
回复
引用 2 楼 lhfslhfs 的回复:
bool bijiao1(int a,int b) { return a>b; }
谢谢 我试了,确实可以,我把更复杂的那个改成返回bool型的也通过了, 必须要传返回bool类型的么?原来那个出错的根本原因是什么呢。
青松2 2013-04-24
  • 打赏
  • 举报
回复
bool bijiao1(int a,int b) { return a>b; }
super_admi 2013-04-24
  • 打赏
  • 举报
回复
这应该是C++的东西吧,我估计你调用的是std::sort,而不是C库函数中的sort。 一般来说std中的东西,玩玩都要重载<, >, ==, =等符号。 提示很明白地告诉你:无效的操作符号<
大石头1987 2013-04-24
  • 打赏
  • 举报
回复
sort(a,a+10,bijiao1);这里是使用谓词函数bijiao1去做排序,好像STL中规定 这个谓词函数的2个参数交换位置时不能同时返回true,像你定义的 int bijiao1(int a,int b) { int c = a-b; return c; } 这个函数,如果a = 5 ; b = 4; 那么对于交换a,b后,函数的返回值都是true. 所以会报错,具体如下: http://blog.csdn.net/ghosc/article/details/6550933
赵4老师 2013-04-24
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处。 对学习编程者的忠告: 眼过千遍不如手过一遍! 书看千行不如手敲一行! 手敲千行不如单步一行! 单步源代码千行不如单步对应汇编一行! 单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。 VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。 对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
  • 打赏
  • 举报
回复
引用 4 楼 lhfslhfs 的回复:
引用 3 楼 aftermisak 的回复:引用 2 楼 lhfslhfs 的回复: bool bijiao1(int a,int b) { return a>b; } 谢谢 我试了,确实可以,我把更复杂的那个改成返回bool型的也通过了, 必须要传返回bool类型的么?原来那个出错的根本原因是什么呢。 Standard C++ Library Refe……
女生!!! 头一回见到。。 我们公司的唯一一位女程序,写代码写哭之后,再也没有出现过。。。 x

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧