【C++】函数

By e2hang at 2025-05-05 • 0人收藏 • 42人看过
#include <iostream>
#define S(x) x*x
using namespace std;

typedef int num;

template <typename x , class y>
const y test(const x a, const y arr[]){
	y sum;
	for(int i = 0; i < a; i++){
		sum+=arr[i];
	}
	return sum;
}

int arrange(int n=1){
	return n;
}

inline double set(double arr[]){
	return arr[0];
}

//transform &
void swap_1(int& a,double& b){
	int temp;
	temp = b;
	b = a;
	a = temp;
}

//transform *
void swap_2(int *a, double* b){
	int temp;
	temp = * a;
	* a = * b;
	* b = temp;
	//func equals int *a = p1 
}

int main(){
	int k;
	cin >> k;
	double* p = new double [k];
	for(int i = 0; i < k; i++){
		cin >> p[i];
	}
	auto s = test(k,p);
	cout << "#define test : S(5)" << S(5) <<endl;
	cout << "Template function test : " << s << endl;
	cout << "Inline function test : " << set(p) << endl;
	cout << "Before SWAP : " << k <<" " << p[0];
	cout << endl;
	swap_2(&k , &p[0]);
	cout << "After SWAP : " << k <<" " << p[0] << endl;
	cout << arrange() <<endl;
	return 0;
}

运行结果:

输入:

5

1 1 1 1 1

输出:

#define test : S(5)25

Template function test : 5

Inline function test : 1

Before SWAP : 5 1

After SWAP : 1 5

1


登录后方可回帖

登 录
信息栏
欢迎来到滑稽社论坛!注册会员即可发帖!

你好啊

Loading...