C++回调函数

回调函数定义:函数作为参数,而发起的函数调用过程称为回调函数。

// _回调函数C++.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"stdlib.h"
#include <stdio.h>
using namespace std;
int myCmp(int a, int b)
{
 if (a>b)
  return 1;
 else
  return 0;
}

void selectSort(int *p, int n, int(*myCmp)(int, int))
{
 for (int i = 0; i<n - 1; i++)
 {
  for (int j = i + 1; j<n; j++)
  {
   if (myCmp(p[i], p[j]))
   {
    p[i] = p[i] ^ p[j];
    p[j] = p[i] ^ p[j];
    p[i] = p[i] ^ p[j];
   }
  }
 }
}
int _tmain(int argc, _TCHAR* argv[])
{
 int a[10] = { 1, 2, 3, 4, 5, 0, 9, 8, 7, 6 };
 selectSort(a, 10, myCmp);
 for (int i = 0; i<10; i++)
  printf("%d ", a[i]);
 system("pause");
 return 0;
 return 0;
}

 

posted @ 2016-07-29 11:51  lhuan  阅读(298)  评论(0编辑  收藏  举报