Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
here goes my code.
i have posted the problems and my tried things in the comments . please do check it.
Thanks.
C#
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;
}


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.
Posted
Updated 7-Jun-16 10:04am
Comments
Philippe Mori 7-Jun-16 20:41pm    
As indicate in solution 1, it is not possible. You can always create a converter class that would allows to adapt existing functions so they can be used...

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900