transform()中使用的函数变化中的问题(stl C ++) [英] Problem in the variation of function used in transform() (stl C++)

查看:196
本文介绍了transform()中使用的函数变化中的问题(stl C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码。

我在评论中发布了问题和我尝试过的东西。请检查一下。

谢谢。

here goes my code.
i have posted the problems and my tried things in the comments . please do check it.
Thanks.

int  change(int x)
{
	//this function works as transform last argument and multiplies all element of vector a by 10 and store them into b.
	x = x * 10;
	return x;
} 
void change_ref(int &x)
{
	//this function doesn't work as transform last argument.
	//error shown : binary =... no operator takes a right hand operand of type void ( or there is no acceptable conversion)
	// what i want to do is to pass by reference the values so that i dont have to return anything.
	x = x * 10;
}
int main()
{
	vector<int>a = { 1,2,3 };
	vector<int>b;
	transform(a.begin(), a.end(), back_inserter(b),change);
	for (int i = 0; i < b.size(); i++)
	{
		cout << b[i] << endl;
 	}
	//b : {10,20,30}  -->desired output.
	return 0;
}





我的尝试:



i已经尝试过以下CHANGE函数,它完全正常工作..即使在c ++ 11中我也尝试过使用lambda函数也很完美。我只是想知道如何传递这些值引用,以便我不必返回任何函数。



What I have tried:

i have tried doing the following CHANGE function that works perfectly fine ..even in c++11 i have also tried using the lambda function that also works perfectly fine .i just want to know how to pass these values by reference so that i dont have to return anything back to the function.

推荐答案

transform (参见文档: transform - C ++ Reference [ ^ ])需要一个一元函数,它接受一个由InputIterator指向的类型的元素作为参数,并且返回某些结果值可转换为OutputIterator 指向的类型,因此不允许返回任何内容的函数。
transform (see the documentation: transform - C++ Reference[^]) requires a "Unary function that accepts one element of the type pointed to by InputIterator as argument, and returns some result value convertible to the type pointed to by OutputIterator so a function returning nothing is not allowed.


这篇关于transform()中使用的函数变化中的问题(stl C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆