C++函数学习

1. 内联函数 inline和define

内联函数比常规函数执行速度更快,比define更安全。

常规函数调用内存中是跳来跳去的,内联函数是直接创建副本,顺序执行,代价是内存浪费更多,define是宏替换。

在不支持inline特性的环境下,经常使用define来达到类似的效果,如单片机。

2.引用变量&、const、&&

常规函数是按值传递的,如在C语言中要修改参数的值只能通过指针来修改,在C++中新增加了&引用,直接访问原值,而不是拷贝。

引用在效率上更高,用的内存更少(因为不用拷贝一次原来的值)。

当想使用引用又不希望参数值被改变时,可以使用const int&。

当引用的只希望是右值则可以使用&&

3.左值与右值

左值指的是可以通过地址修改的。

右值指的是不可以通过内存地址修改的。

int a=1+2;

a是左值,1+2是右值

int a=0,b=2;

a是左值,b+2是右值,因为b+2是产生一个临时变量

4.return 和 return&

return 返回一个临时复制的值

return & 返回引用值,即可以当左值。只可以返回一个存在的内存,而不能返回一个已经被销毁的内存。

在效率和内存上和参数的引用一个原理。

5.引用主要原因有两个

1.是可以修改原值

2.提高运行效率。

主要是用在占据内存较大(和基本的数据类型比较的话)的类和数据结构。

6.函数重载

名称相同,功能类似,参数列表不同的时候使用。

7.函数模板

算法一样,类型不一样的时候使用。

模板的原理:

编译器在编译的时候会检查模板的使用情况,创建不同的实例,程序运行过程中是没有模板的。

当使用模板的时候不知道计算结果的类型时,可以使用decltype。会遍历一个核对表,选择最适合的类型。

8.递归

函数本身调用自己来工作,直到遇到停止条件时停止工作。

9.函数指针

接下来是代码实例

#include<iostream>
using namespace std;
#define Squart(X) (X)*(X)
inline double squart(double a) { return a*a; }
struct person
{
	char name[20];
	int age;
	void setName(const char *);
	person();
	person(const char *n, int a);
	void show()
	{
		std::cout << "my name is " << name << " and i am " << age << (age == 1 ? " year" : " years") << " old" << endl;
	}
};
void person::setName(const char * n)
{
	int index = 0;
	do
	{
		name[index] = *(n + index);
	} while (n[index++]);
}
person::person()
{
	setName("Big ben");
	age = 20;
}
person::person(const char*n, int a)
{
	setName(n);
	age = a;
}
template<typename T>
void swap_(T &a, T& b)
{
	T temp = a;
	a = b;b = temp;
}
template<class T1,class T2>
void add(T1 A, T2 B)
{
	decltype(A+B) k = A + B;
	cout << k << endl;
}
int & inadd(int &result, const int &a, const int &b)
{
	result = a + b;
	return result;
}
int main() {
	double a = 0;
	cin >> a;
	cout << "define "<< Squart(a)<<endl;
	cout << "inline " << squart(a) << endl;
	person *p = new person("bai",30);
	p->show();
	p->setName("a little girl");
	p->show();
	int inta = 0,intb = 2;
	char cha = 'a', chb = 'b';
	cout << "int" << endl << inta << " " << intb << endl;
	swap_(inta, intb);
	cout << "int" << endl << inta << " " << intb << endl;

	cout << "char" << endl << cha << " " << chb << endl;
	swap_(cha, chb);
	cout << "char" << endl << cha << " " << chb << endl;

	add(1, 2);
	add(1.30, 5);
	add(1.000021, 1);
	int r = 0, m = 2, n = 1;
	inadd(r, m, n);
	cout << r << endl;
	inadd(r, m, n) += 2;
	cout << r << endl;
	return 0;
}

	

函数指针代码

#include<iostream>
using namespace std;
double add(double a, double b) { return a + b; }
double sub(double a, double b) { return a - b; }
double mul(double a, double b) { return a*b; }
double calculate(double a, double b, double(*p)(double c, double d)){	return (*p)(a, b);}
int main()
{
	double(*p[3])(double, double) = { add,sub,mul };
	double a, b;
	cout << "----------------------------------------- " << endl;
	cout << "a\tb\ta+b\ta-b\ta*b\t" << '|'<<endl;
	cout <<  "---------------------------------------" << endl;
	while (cin>>a>>b)
	{
		//system("cls");
		cout << a << '\t' << b ;
		for (int i = 0;i < 3;i++)
			cout << "\t" << calculate(a, b, p[i]);
		cout <<endl<<"---------------------------------------"<<endl;
	}
	return 0;
}

递归代码

#include<iostream>
using namespace std;
#define W 10
#define H 11
//int T = 0;
int mode[][2] = { {-1,1},{1,-1} };//0坐下1右上
int jz[H][W] = { {0} };
int out[H*W] = {};
int jz2[H][W] = { {0} };
int index = 0;
void Zcout(int m, int nx, int ny, int f, int &p)
{
	if (nx == 0 && ny == 0) p = 0;
	if (nx >= W || ny >= H) return;
	else jz[ny][nx] = p++;
	if (ny == 0 && nx % 2 != f && nx!=W-1){
		nx++;Zcout(!m, nx, ny,f,p);
	}
	else if (nx == 0 && ny % 2==f && ny !=H - 1){
		ny++;Zcout(!m, nx, ny, f,p);
	}
	else if (nx == W - 1 && ny%2==W%2+f){
		ny++;Zcout(!m, nx, ny, f, p);
	}
	else if (ny == H - 1 && nx % 2 != H % 2+f) {
		nx++;Zcout(!m, nx, ny, f, p);
	}
	else
	{	
		nx += mode[m][0];
		ny += mode[m][1];
		Zcout(m, nx, ny,f, p);
	}
}
void Zcin(int m, int nx, int ny, int f)
{
	if (index==W*H) return;
	else out[index++] = jz[ny][nx];
	if (ny == 0 && nx % 2 != f && nx != W - 1) {
		nx++;Zcin(!m, nx, ny, f);
	}
	else if (nx == 0 && ny % 2 == f && ny != H - 1) {
		ny++;Zcin(!m, nx, ny, f);
	}
	else if (nx == W - 1 && ny % 2 == W % 2 + f) {
		ny++;Zcin(!m, nx, ny, f);
	}
	else if (ny == H - 1 && nx % 2 != H % 2 + f) {
		nx++;Zcin(!m, nx, ny, f);
	}
	else
	{
		nx += mode[m][0];
		ny += mode[m][1];
		Zcin(m, nx, ny, f);
	}
}
int main()
{
	int k = 0;
	Zcout(0,0,0,0,k);//mode==f 1向上 0向下 
	for (int i = 0;i < H;i++)
	{
		for (int j = 0;j < W;j++)
			cout << jz[i][j] << "\t";
		cout << endl;
	}
	Zcin(0, 0, 0, 0);
	for (int i = 0;i < H*W;i++)
		cout << out[i] << " ";
	return 0;
}



猜你喜欢

转载自blog.csdn.net/Dang_boy/article/details/77579849
今日推荐