c++---------函数传递,记录知识(以代码的方式呈现)

void error_msg(initializer_list<int> il)
{
	int sum = 0;
	cout << il.size() << endl;
	for (auto beg = il.begin(); beg != il.end(); ++beg)
	{
		sum += *beg;                              //initializer_list对象中的元素是常量值
		
	}
		
	cout << sum;
}


int main()
{
	string expected("das");
	string actual("dfsa");
	error_msg({ 1,2,3 });   //初始化必须要有大括号;
	                             //initializer_list是一个传入
	                      //可变形参的好方法,也就是初始化提供了方便

	
	//return 0;
	system("pause");
}


//列表初始化返回值
vector<string>  process()
{
	string f("asdasda");
	return { f ,"asd"};  //注意这里的花括号,直接给一个vector对象赋初值
}

int main()
{
	string s;
	s=process();   //通过这种方式就可以返回一个vector的函数
}

       //函数的递归调用
   //计算val的阶乘
int factorial(int val)
{
	if (val > 1)
		return factorial(val - 1)*val;
	return 1;
}

bool str_subrrange(const string &str1,const string &str2)
{
	if (sr1.size()==sr2.size())
		return str1==str2;
	auto size=(sr1.size()<sr2.size())?str1.size():str2.size();
	for(decltype(size) i=0;i!=size;++i)
	if(str1[i]!=str2[i])
	return;
}

//递归输出一个vector中的所有内容
void _get(vector<int> s,int index)
{
	if (index-1 > 0)
	{
		cout << s.at(index-1)<<endl;
		 _get(s, index - 1);
	}
	else cout<< s.at(0)<<endl;


}


/*
	 返回数组指针的用法
	                     */
//arrT是一个类型别名,它表示的类型是含有10个整数的数组
	//typedef int arrT[10];
	using arrT = int[10];
	arrT* func(int i);

	//注意这三者的区别
	int arr[10];   //arr是一个含有10个整数的数组
	int *p1[10];   //p1是一个含有10个指针的数组(是整数型的)
	int (*p2)[10];   //p2是一个指针,它指向含有10个整数的数组

//这里对于理解指针很重要
	cout << *(arr+1)<<endl;    //arr是这个数组的地址
	cout << *(*(&arr)+2)<<endl;    //是数组元素的首地址
	cout << (&arr[0])<<endl;     //arr相当于&arr[0]也相当于&arr
	cout << *&arr << endl;    //表示的还是arr[0]的地址

	/*..........................*/
	int a = 10;
	int *u;
	int *(&p) = u;    //从内向外看,p首先是个引用,其次是个指针的引用,指向u
	p = &a;
	cout << *u;  
	/*..........................*/
	//这里是个函数的关于返回数组的指针引用
    int (*(&func(int i)))[10]     //从内向外看,首先是个引用,其次是个指针引用,该指针指向一个10个整数的数组
   {
	
   }

   /*............................*/
	int y[3]{ 1,2,3 };
	int(&t1)[3] = y;
	cout << t1[0] << endl;
	cout << t1[1] << endl;
	cout << t1[2] << endl;
/*............................*/
	string yp[10];
	string (&func(int i))[10];   //从内向外看,首先是个引用,其次是个指针引用,该指针指向一个10个整数的数组
	using fund = string[10];
	fund  &fundc();   //使用类型别名,创建一个引用
	decltype (yp)  &fiu();    //创建一个引用
	decltype (yp)  *fung();   //使用decltype关键字,创建一个指向10个string类型数组的指针;

 /*............................*/
   int odd[10];
   int ard[10];
   decltype (odd) *young(int i)
   {
	   return (i % 2) ? &odd : &ard;   //使用decltype时,decltype(odd)的结果是一个数组,因此返回指针,
	                                  //必须在函数声明时加一个*符号
    }

    /*............................*/
	//函数指针,顾名思义返回的是一个函数指针
	
	//比较两个string对象的长度
	bool lengthCompare(const string&, const string&);
	//声明一个指向该函数的指针,只需要用指针替换函数名即可:
	//pf是一个指向函数的指针,该函数的参数是两个const string 的引用,返回值是bool类型
	bool (*pf1)(const string&, const string&);//此时未初始化
	pf1 = lengthCompare;           //pf指向lengthCompare,lengthCompare是个地址
	pf1 = &lengthCompare;         //等价调用.......注意进行拷贝时,需要进行精确匹配
	//为避免写的这么冗长,可以使用decltype
	decltype(lengthCompare)  *pf1;//是一个bool函数类型的指针;
	/*............................*/
	//函数指针形参
	void useBigger(const string &s1, const string &s2, bool(*fd)(const string&, const string&));
	//为避免写的这么冗长,可以使用decltype和类型别名 
	typedef bool (*pf) (const string&, const string&); //pf是一个返回bool类型的函数指针
	typedef   decltype(lengthCompare)  *pf;
	void useBigger(const string &s1, const string &s2,  pf);  //我觉得加typedef没意思
	/*............................*/
	//返回指向函数的指针
	using F = int(int*, int);//F是一个函数类型
	using pF = int(*)(int*, int);    //看右边形参列表,是一个函数,先是括号里的,
	                                 //它是一个指针,所以是一个函数指针
	            //首先pF是一个类型,因此,pF是一个指向函数类型的指针,它返回int
	//显示的表达
	int(*f1(int))(int *, int);
	//下面这种看法更加简单,明了
	string::size_type sumlength(const string&, const string&);
	decltype (sumlength) *f;   
    /*............................*/
    template <typename T>  //模板声明是在函数外

     inline int funt_plus(int a, int b)
       {
	         return a + b;
       }
     inline int funt_minus(int a, int b)
       {
	         return a - b;
       }
     inline int funt_multip(int a, int b)
       {
	         return a * b;
       }
     inline double funt_divide(double a, double b)
       {
	         return a / b;
       }
	T funt(T,T);
	typedef decltype(funt) *fwe;
	vector<fwe>   dsa;
	using io = int(*)(int, int);
	vector<io> ff;
	
	dsa.push_back(funt_plus);
	dsa.push_back(funt_minus);
	dsa.push_back(funt_multip);
	dsa.push_back(funt_divide);   //使用参数模板,这样除法就可以用double类型     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值