Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Pass data of Vector<Vector<double>> into a function call
Pass data of Vector<Vector<double>> into a function call [message #56482] Wed, 17 March 2021 08:13 Go to next message
sinpeople is currently offline  sinpeople
Messages: 29
Registered: October 2020
Location: Singapore
Promising Member

Hi folks,

I need to pass a data variable of type Vector<Vector<double>> into a function call. What's the best way to do it?
Inside the function call, a clone will be created according to the data passed in, without modifying the value passed in.

Any example of general guidelines on doing this? Thank you!

Best Regards
David WANG
Re: Pass data of Vector<Vector<double>> into a function call [message #56487 is a reply to message #56482] Wed, 17 March 2021 21:53 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Well quite simple question:

just put:
void function( const Vector<Vector<double>>& var);
Re: Pass data of Vector<Vector<double>> into a function call [message #56488 is a reply to message #56482] Wed, 17 March 2021 23:57 Go to previous messageGo to next message
Xemuth is currently offline  Xemuth
Messages: 387
Registered: August 2018
Location: France
Senior Member
Hello,

if you want to clone your vector, you can simply use clone :

This exemple show a function taking a vector<vector<double>> in entry cloning it, add 1 to all of double, then return it by moving the clone.
#include <Core/Core.h>

using namespace Upp;

Vector<Vector<double>> PrintVector(const Vector<Vector<double>>& vector);


CONSOLE_APP_MAIN
{
	//original vector of vector of double
	Vector<Vector<double>> original{{2.0,3.0},{4.0,5.0}};
	
	//pick allow you to move a value between variable without having to do copy
	Vector<Vector<double>> ret = pick(PrintVector(original));
	
	//We print the vector and all is double
	for(const Vector<double>& vec : ret){
		for(const double& d : vec){
			Cout() << d;
		}
		Cout() << "\n";
	}
}

Vector<Vector<double>> PrintVector(const Vector<Vector<double>>& vector){
	//Clone allow you to clone a variable (the class you want to copy must have a copy constructor)
	Vector<Vector<double>> myClone = clone(vector);
	
	//We add + 1 to all double of our clone
	for(Vector<double>& vec : myClone){
		for(double& d : vec){
			d += 1.0;
		}
	}
	
	//we return our clone not by copying but by moving it
	return pick(myClone);
}

Re: Pass data of Vector<Vector<double>> into a function call [message #56500 is a reply to message #56488] Fri, 19 March 2021 13:26 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Sometimes it is good to use r-value reference, as it gives you freedom in usage:

Vector<Vector<double>> AdjustVector(Vector<Vector<double>>&& vector){
	for(Vector<double>& vec : vector){
		for(double& d : vec) {
			d += 1.0;
		}
	}
	return pick(vector);
}

CONSOLE_APP_MAIN
{
	Vector<Vector<double>> x { { 1, 2 }, { 3, 4 } };
	
	auto y = AdjustVector(clone(x));
	
	DDUMP(x);
	DDUMP(y);
	
	y = AdjustVector(pick(x));

	DDUMP(x);
	DDUMP(y);
}


Mirek
Previous Topic: Multiply a scalar to every element of a vector
Next Topic: FileIn and Buffer cannot be composed in a class?
Goto Forum:
  


Current Time: Thu Mar 28 16:55:36 CET 2024

Total time taken to generate the page: 0.02016 seconds