当前位置:网站首页>Collapse expression

Collapse expression

2022-06-11 13:30:00 Hair like snow ty

Let's take a look at an example , There are three summation functions in this example sum1,sum2,sum3.

auto sum1()
{
    
	return 0;
}

template<typename T, typename...U>
auto sum1(T a, U... rest)
{
    	
	return a + sum1(rest...);
}


template<typename T,typename...U>
auto sum2(T a, U... rest)
{
    
	if constexpr(sizeof...(rest)>0)
	{
    
		return a + sum2(rest...);
	}
	else
	{
    
		return a;
	}
}


template<typename...U>
auto sum3(U... rest)
{
    
	return (... + rest);
}

int main()
{
    
	cout << sum1(1, 2, 3, 5.5, 6) << endl;
	cout<<sum2(1, 2, 3, 5.5, 6) << endl;
	cout << sum3(1, 2, 3, 5.5, 6) << endl;
	system("pause");
	return 0;
}

result :
 Insert picture description here
         In the above code sum3 You use the collapse expression , You can see that the code is very concise , But it also realizes the same function .
The collapse expression is c++17 Standard introduced , The main purpose of introducing folding expression is to calculate a certain value , What makes this value special is : It relates to all variable parameters , It is not related to a single parameter .
         Folding expressions generally have 4 format ( Note that each format is enclosed in parentheses ). Left fold means that parameters are calculated from the left , Turning right means that parameters are calculated from the right .

One yuan is off to the left

Format :(… Operator A package of parameters )
Calculation method :((( Parameters 1 Operator Parameters 2) Operator Parameters 3))… Operator Parameters N)
Example :

template<typename ...U>
auto sub_left(U...paras)
{
    
	return (... - paras);
}

int main()
{
    
	cout << sub_left(10, 20, 30, 40) << endl;
	system("pause");
	return 0;
}

result :-80

One dollar right discount

Format :( A package of parameters Operator …)
Calculation method : Parameters 1 Operator ( Parameters 2…( Parameters N-1 Operator Parameters N))
Example :

template<typename ...U>
auto sub_right(U...paras)
{
    
	return (paras - ...);
}

int main()
{
    
	cout << sub_right(10, 20, 30, 40) << endl;
	system("pause");
	return 0;
}

result :-20

Binary left fold

Format :(init Operator … Operator A package of parameters )
Calculation method :(((init Operator Parameters 1) Operator Parameters 2)… Operator Parameters N)
Example :

template<typename ...U>
auto sub_left(U...paras)
{
    
	return (220 - ... - paras);
}

int main()
{
    
	cout << sub_left(10, 20, 30, 40) << endl;
	system("pause");
	return 0;
}

result :120
((((220-10)-20)-30)-40)

Two yuan right turn

Format :( A package of parameters Operator … Operator init)
Calculation method :( Parameters 1 Operator (…( Parameters N Operator init))
Example :

template<typename ...U>
auto sub_right(U...paras)
{
    
	return (paras - ... - 220);
}

int main()
{
    
	cout << sub_right(10, 20, 30, 40) << endl;
	system("pause");
	return 0;
}

result :200
10-(20-(30-(40-220)))=200

原网站

版权声明
本文为[Hair like snow ty]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111325091454.html