?? func_ptr.cpp
字號:
// func_ptr.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
typedef int (*CMP)(int a, int b) ;
typedef vector<int> Int_Array;
int noless(int a, int b)
{
return a>=b?1:0;
}
int less(int a, int b)
{
return a<b?1:0;
}
void bubble_sort(Int_Array& base,CMP cmp)
{
for(int i=base.size()-1;i>0;i--)
for(int j=0;j<i;j++)
{
if(cmp(base[j],base[j+1]))
{
int temp=base[j];
base[j]=base[j+1];
base[j+1]=temp;
}
}
}
int main(int argc, char* argv[])
{
#if 1
int (*mf)(int a,int b);
mf=noless;
mf=&less;
cout<<(*mf)(3,4)<<endl;
#endif
#if 0
Int_Array base;
base.reserve(10); //預留10個元素的空間,這樣可以避免push_back引起的內(nèi)存重分配,優(yōu)化性能
base.push_back(6);
base.push_back(4);
base.push_back(12);
base.push_back(9);
base.push_back(8);
base.push_back(1);
bubble_sort(base,less) ; // 通過賦值來改變bubble_sort的排序方向
for(int i=0;i<base.size();i++)
cout<<base[i]<<"\t";
#endif
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -