标签:efi 替换 style pac 类型 highlight sha ace font
内联函数是内联扩展是用来消除函数调用时的时间开销。它通常用于频繁执行的函数
如需要频繁执行某个操作,可以使用inline来进行定义。
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <shappmgr.h>
#include <stdio.h>
using namespace std;
inline bool isnumber(char ch) {
return ((ch >= ‘0‘) && (ch <= ‘9‘));
}
int main() {
char a;
while ((a = cin.get()) != ‘\n‘) {
if (isnumber(a))
{
cout << "你输入了一个0-9之间的数字" << endl;
}
else {
cout << "你输入了一个非0-9之间的数字" << endl;
}
}
return 0;
}
但是内联函数不能含有复杂的关键词(如while,switch),如有那么编译器将无视这些定义,继续为下面的调用产生同样的调用代码。
递归函数不能用于内联函数,
很多情况下,内联函数被限制在小型的并频繁调用的函数上。
内联函数与宏定义
#define MAX(a,b)((a)>(b)?a:b))
可使用内联函数替换为:
inline int MAX(int a,int b){
return a>b?a:b
}
示例
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <fstream>
#include <shappmgr.h>
#include <stdio.h>
using namespace std;
inline int tobiger(int a, int b) {
return a > b ? a : b;
}
inline int tosmall(int a, int b) {
return a < b ? a : b;
}
int main() {
int a = 10;
int b = 11;
cout << tobiger(a,b)<< endl;
cout << tosmall(a, b) << endl;
a = a + a;
cout << tobiger(a++, b) << endl;
cin.get();
return 0;
}
输出:
11
10
20
标签:efi 替换 style pac 类型 highlight sha ace font
原文地址:https://www.cnblogs.com/fuRyZ/p/8909077.html