C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 27th, 2008, 11:10 AM
sheree sheree is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 6 sheree User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 36 m 34 sec
Reputation Power: 0
Question Pass and return vector from/to function c++

Hello everyone,
I have been working on dynamic arrays, so for this I used vector for this purpose, but now I would like to get some advices about how can I pass and return vector to/from function, what I wanted to do is as follows:

1. pass a vector into function1
2. set the size of it and fill it inside function1
3.return the new vector from function1
4.function1 is inside function2

so, that means:


Code:
//-------------------
#include< whatever you want >
#include< vector >

//protype functions 1,2
//-------------------
vector <type  > function1(vector < type > *); 
vector <type  > function2(vector < type > *);
//--------------------

Code:
int main()
{
vector < type > v1 //will be used as input
vector < type > v2 //will be used as output

v2=function2(&v1);

return(0);

}


Code:
vector < type > function2(vector < type > * vec1)
{
vector< type > vec2;
int size=10;//the size of the vector 
vector < type > vec1(size);

vec2=function1(vec1);//this what I did
vec2=function1(&vec1);//or by address
return(vec2); //this what I did  
return(*vec2); //or as pointer                                                          
} 


Code:
vector< type > function1(vector < type > *vec)
{
    vector < type > vectemp;
  //take vec do something 
   // and get new vector with
  //new size 
return(vectemp);
}


actually The way, I did,didn't work, where I had only empty vector2 and the suggestion that I wrote doesn't work .....
well, I don't know whay ....
Any kind of help would be appreciated.

Sheree

Reply With Quote
  #2  
Old May 28th, 2008, 09:09 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
Code:
#include <vector>
#include <iostream>

using namespace std;

void function1(vector<int> &v);
void function2(vector<int> &v);

int main()
{
	vector<int> test;
	function1(test);

	cout<<"size:"<<test.size()<<endl;
	cout<<"contents: ";
	for(int i = 0; i < test.size(); i++)
	{
		cout<<test[i]<<" ";
	}
	cout<<endl;
	return 0;
}

void function1(vector<int> &v)
{
	v.push_back(3);
	v.push_back(4);
	function2(v);
}

void function2(vector<int> &v)
{
	v.push_back(5);
	v.push_back(6);
}

small example of how to pass vectors by reference
__________________

Last edited by Bobidybob : May 28th, 2008 at 09:21 AM.

Reply With Quote
  #3  
Old May 28th, 2008, 10:31 AM
sheree sheree is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 6 sheree User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 36 m 34 sec
Reputation Power: 0
Question

thanks for the replay.
But that thing I know, and I need a function that returns a vector.
Actually I posted just an example to pose you in my problem
and I didn't want you to get confusion if I sent the all project I am working on.
So I need this vector because I want to manipulate the size of the array inside the function and return the result vector with its new size easily.
Thanks again, and any other suggestions will be appreciated


Sheree

Reply With Quote
  #4  
Old May 28th, 2008, 11:05 AM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
okies

Code:
#include <vector>
#include <iostream>

using namespace std;

vector<int> function1(vector<int> v);

int main()
{
	vector<int> test;
	vector<int> test2;
	test2 = function1(test);

	cout<<"size:"<<test2.size()<<endl;
	cout<<"contents: ";
	for(int i = 0; i < test2.size(); i ++)
	{
		cout<<test2[i]<<" ";
	}
	cout<<endl;
	return 0;
}

vector<int> function1(vector<int> v)
{
	v.resize(20);
	for(int i = 0; i < 20; i++)
	{
		v[i] = i * 3;
	}

	return v;
}

Reply With Quote
  #5  
Old May 28th, 2008, 11:58 AM
sheree sheree is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2008
Posts: 6 sheree User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 36 m 34 sec
Reputation Power: 0
thanks again for the quick answer.
Yes you are right.
but could you tell me what is the differnce between passing the vector itself or just a pointer of it

Reply With Quote
  #6  
Old May 28th, 2008, 12:31 PM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 336 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 15 h 51 m 40 sec
Reputation Power: 12
Send a message via AIM to Bobidybob
passing by reference will change the original whereas passing the object itself will only make a copy of the original. no matter what you do to the copy, the original will stay the same. it all depends on what you're trying to accomplish as to which method you use.

Reply With Quote
  #7  
Old February 27th, 2009, 07:16 AM
tej tej is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 1 tej User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 49 sec
Reputation Power: 0
error while executing above code

I compiled the suggested code in g++ 4.3 and received the following message. Please help me on how I should proceed. (The message was longer.I have shortened it due to restrictions on the message size)

/tmp/cccGFKhO.o: In function `__static_initialization_and_destruction_0(int, int)':
vector1.cpp.text+0x1d): undefined reference to `std::[/FONT]ios_base::Init::Init()'
vector1.cpp.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cccGFKhO.o: In function `demo(int)':
vector1.cpp.text+0x82): undefined reference to `std::allocator<char>::allocator()'

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Pass and return vector from/to function c++


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap