主要就是别名,之前一字节用的#define unsigned char u8,四字节用#define unsigned int u32
用define简单的替换,可能会出问题
- 数据类型
- Uint8_t uint32_t等定长的定义,
- List<int>这种复杂类型
- 函数指针的定义,很好地简化
#include <iostream>
#include <stdint.h>
#include <list>
using namespace std;
#define U8 unsigned char
#define U8P unsigned char *
typedef uint8_t u8;
typedef uint8_t* u8p;
typedef list<int> listint;//定义list,复杂类型
typedef void (*funp)(int x);//定义指针
void Fun(int x){
cout<<"in Fun x="<<x<<endl;
}
int main(){
U8 a,b;//用define这种直接替换可能没问题,
a='a';
b='b';
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
U8P c,d;//unsigned char *c,d;这样c就是指针而d是char,define的替换就会出问题
c=&a;
d=b;
cout<<"*c="<<*c<<endl;
cout<<"d="<<d<<endl;
u8 e='e';
cout<<"e="<<e<<endl;
u8p f,g;//typedef则无问题
f=&a;
g=&b;
cout<<"*f="<<*f<<endl;
cout<<"*g="<<*g<<endl;
listint l;//复杂类型
l.push_back(1);
funp p_Fun;//函数指针
p_Fun=Fun;
p_Fun(888);
return 0;
}
Inline函数即内联函数,编译器会在调用处直接展开,提高程序速度和节约栈空间
用于简单的函数,比如pair源码的==重载
- inline bool operator==(const pair<T1, T2>& x, const pair<T1, T2>& y) {
- return x.first == y.first && x.second == y.second;
- }
常用的swap这种适合声明为Inline