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 » Passing Vectors as arguments of functions.
Passing Vectors as arguments of functions. [message #28299] Thu, 26 August 2010 18:54 Go to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
Hi,

A simple query, can it be a Vector (or any other template) passed as an argument of a function? Are there some limitations?

Cheers,

Javier.
Re: Passing Vectors as arguments of functions. [message #28300 is a reply to message #28299] Thu, 26 August 2010 19:12 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1092
Registered: August 2007
Senior Contributor
Hi,

Yes you can use vectors (or other templates) as arguments, and it is very simple. If you already have the UPP package installed, just check out the bazaar/rss package. It uses vectors and vectormaps as arguments Smile

Regards.


Re: Passing Vectors as arguments of functions. [message #28301 is a reply to message #28300] Thu, 26 August 2010 20:29 Go to previous messageGo to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
Rss code is not available in bazaar (the link does not work).

Javier
Re: Passing Vectors as arguments of functions. [message #28302 is a reply to message #28301] Thu, 26 August 2010 20:43 Go to previous messageGo to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
for example, in this simple code:
#include <iostream>
#include <CtrlLib/CtrlLib.h>
using namespace std;
using namespace Upp;


struct A:Moveable<A>{
		
	int x,y,z;
	A(int x1, int y1, int z1){x=x1;y=y1;z=z1;}
	
};

void multi (Vector<A> a, Vector<A> b, Vector<A> c);


int main(){
	
	Vector<A> vector_A1,vector_A2,vector_A3;
	
	vector_A1.Add(A(10,20,30));
	cout<<vector_A1[0].x<<'\n';
	cout<<vector_A1[0].y<<'\n';
	cout<<vector_A1[0].z<<'\n';
	
	vector_A2.Add(A(40,50,60));
	
	multi(vector_A1,vector_A2,vector_A3);
	//cout<<vector_A3[0].x<<'\n';
	//cout<<vector_A3[0].y<<'\n';
	//cout<<vector_A3[0].z<<'\n';
	
	
	return 0;	
}


void multi (Vector<A> a, Vector<A> b, Vector<A> c){
	
	c.Add(A(a[0].x*b[0].x,a[0].y*b[0].y,a[0].z*b[0].z));
	
}


why values are not passed to vector_A3? is it correct the way of passing the Vectors?

Cheers,

Javier
Re: Passing Vectors as arguments of functions. [message #28303 is a reply to message #28302] Thu, 26 August 2010 20:45 Go to previous messageGo to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
I realise that vector_A3 has to be passed by reference.
Re: Passing Vectors as arguments of functions. [message #28304 is a reply to message #28301] Thu, 26 August 2010 21:01 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1092
Registered: August 2007
Senior Contributor
A simple example code (nevermind its absurdity):

Vector<int> index;
VectorMap<String, String> person;


void Foo::AddPersons(Vector<int>& i, VectorMap<String, String>& p)
{
bla bla...
}

// In your application, all you have to do is to call the function/method properly.

Foo::AddPersons(index, person);



And here is some information about containers on UPP

http://www.ultimatepp.org/srcdoc$Core$Tutorial$en-us.html

http://www.ultimatepp.org/srcdoc$Core$Caveats$en-us.html

I hope this will help you.


And FYI, Rss example is in the UPP package/bazaar section. Not on the web page.

Regards.


Re: Passing Vectors as arguments of functions. [message #28305 is a reply to message #28304] Thu, 26 August 2010 22:39 Go to previous messageGo to next message
281264 is currently offline  281264
Messages: 270
Registered: June 2010
Location: Spain
Experienced Member
Roger!
Re: Passing Vectors as arguments of functions. [message #28307 is a reply to message #28305] Thu, 26 August 2010 23:18 Go to previous message
cbpporter is currently offline  cbpporter
Messages: 1401
Registered: September 2007
Ultimate Contributor
You should always pass containers as a reference, otherwise you risk loosing the content of your original container.
Previous Topic: Assert failed in core/value.h line 464
Next Topic: Making applications more fancy
Goto Forum:
  


Current Time: Fri Apr 19 11:52:47 CEST 2024

Total time taken to generate the page: 0.02629 seconds