当前位置:网站首页>"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
2022-07-03 05:21:00 【Dongli_】
Be careful : This is an original article , Without consent , Please do not reprint at will .
1. Questions and ideas
Q: Implement a type independent comparison function
A: Declare a function pointer , Each type implements its own comparison function , The function pointer points to the comparison function of a specific type , You can realize functions similar to templates ;
One thing that deserves special attention : The formal parameter type in the function pointer is declared as void, In this way, any type can be passed in , in other words , Parameters passed to function pointers are pointers to certain types of data , such , There are no restrictions on the type of input parameters ~*
Upgraded version : In fact, you can also consider templates , Reduce duplicate code , See the next blog :《C And a pointer 》—— The first 13 Chapter function pointer function 1: Callback function 2( Combined with formwork , Simplify the code )
2. Concrete realization
Realized int、char、string、 Class type comparison function .
#pragma once
#include <iostream>
#include <string>
using namespace std;
// Write a type independent comparison function , Note that it is not a template ,
// Method : Declare a function pointer , Each type implements its own comparison function , The function pointer points to the comparison function of a specific type , You can realize functions similar to templates .
int(*compare)(const void*, const void*);
/* Agree that the return value of a specific type represents the meaning , return 0: equal ; return -1: Parameters 1< Parameters 2 return 1: Parameters 1> Parameters 2; */
int compare_int(const void* a1, const void* a2)
{
if (*(int*)a1 < *(int*)a2)// First the void* Convert to int*; Then dereference * Take the value in the address indicated by the pointer
{
return -1;
}
else if (*(int*)a1 == *(int*)a2)
{
return 0;
}
else
{
return 1;
}
}
int compare_char(const void* a1, const void* a2)
{
if (*(char*)a1 < *(char*)a2)
{
return -1;
}
else if (*(char*)a1 == *(char*)a2)
{
return 0;
}
else
{
return 1;
}
}
int compare_string(const void* a1, const void* a2)
{
if (*(string*)a1 < *(string*)a2)
{
return -1;
}
else if (*(string*)a1 == *(string*)a2)
{
return 0;
}
else
{
return 1;
}
}
int compare_Student(const void* a1, const void* a2);
class Student
{
public:
Student() :name(""), score(0) {
}
Student(const string& _name, const int& _score) :name(_name), score(_score) {
}
friend ostream& operator<<(ostream& os, const Student& stu)
{
os << stu.name << "\t" << stu.score;
return os;
}
friend int compare_Student(const void* a1, const void* a2)
{
if ((*(Student*)a1).score < (*(Student*)a2).score)
{
return -1;
}
else if ((*(Student*)a1).score == (*(Student*)a2).score)
{
return 0;
}
else
{
return 1;
}
}
private:
string name;
int score;
};
void TestFunctionPointer()
{
int a[] = {
4,2,5 };
char chars[] = "ascii";
string s[] = {
"Anne","Zoe","Mary" };
Student stus[] = {
{
"Anne",80},{
"Zoe",95},{
"Mary",90} };
cout << " Function pointer to int Type comparison function " << endl;
int nCountA = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < nCountA; ++i)
{
cout << a[i] << "\t";
}
cout << endl;
compare = compare_int;
int *pa = a;
while (pa != a + nCountA - 1)
{
cout << compare(pa++, pa) << endl;
}
cout << endl;
cout << " Function pointer to char Type comparison function " << endl;
int nCountChar = sizeof(chars) / sizeof(chars[0]);
int nTmp = nCountChar - 1;
char *pc = &chars[0];
while (nTmp--)
{
cout << *pc++;
}
cout << endl;
compare = compare_char;
pc = &chars[0];
while (pc != &chars[nCountChar - 2])
{
cout << compare(pc++, pc) << endl;
}
cout << endl;
cout << " Function pointer to string Type comparison function " << endl;
int nCountS = sizeof(s) / sizeof(s[0]);
for (int i = 0; i < nCountS; ++i)
{
cout << s[i] << endl;
}
compare = compare_string;
string *ps = &s[0];
while (ps != &s[nCountS - 1])
{
cout << compare(ps++, ps) << endl;
}
cout << endl;
cout << " Function pointer points to class type Student The comparison function of " << endl;
int nCountStus = sizeof(stus) / sizeof(stus[0]);
for (int i = 0; i < nCountStus; ++i)
{
cout << stus[i] << endl;
}
compare = compare_Student;
Student *pStus = &stus[0];
while (pStus != &stus[nCountStus - 1])
{
cout << compare(pStus++, pStus) << endl;
}
}
3. Results screenshots

边栏推荐
- Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
- [set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)
- 1095 cars on campus (30 points)
- Skip table: principle introduction, advantages and disadvantages of skiplist
- Altaro VM backup getting started
- leetcode860. Lemonade change
- How to connect the network: Chapter 1 CSDN creation punch in
- @Autowired 导致空指针报错 解决方式
- (subplots用法)matplotlib如何绘制多个子图(轴域)
- AtCoder Beginner Contest 258(A-D)
猜你喜欢

How do I migrate my altaro VM backup configuration to another machine?

Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN

How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in

Export the altaro event log to a text file

leetcode435. Non overlapping interval

Redis使用Lua脚本简介

6.23 warehouse operation on Thursday

Audio Focus Series: write a demo to understand audio focus and audiomananger

Go practice -- gorilla / websocket used by gorilla web Toolkit

Make your own dataset
随机推荐
cookie session jwt
Making coco datasets
Explanation of several points needing attention in final (tested by the author)
Transferring images using flask
Introduction to deep learning (II) -- univariate linear regression
BIO、NIO、AIO区别
Deploy crawl detection network using tensorrt (I)
Rust基础入门之(基本类型)
Yolov5 input (II) | CSDN creative punch in
Robot capture experiment demonstration video
Go practice -- gorilla / websocket used by gorilla web Toolkit
@Autowired 导致空指针报错 解决方式
获取并监控远程服务器日志
Brief introduction of realsense d435i imaging principle
Redis 击穿穿透雪崩
JS function algorithm interview case
Class loading mechanism (detailed explanation of the whole process)
Audio Focus Series: write a demo to understand audio focus and audiomananger
Export the altaro event log to a text file
Celebrate the new year together