⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 func_ptr.cpp

📁 一个函数指针的例子,C++语言,我在初学C++时写的,可以参考一下
💻 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引起的内存重分配,优化性能
	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 + -