By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,666 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,666 IT Pros & Developers. It's quick & easy.

Pass vector into a function

P: n/a
sci
class A;

void function(vector<A>* l)
{
....
}

main(){
vector<A> aAList;
function(&aAList[0]);
}

How to pass address of aAlist object into a function? How to call each
member inside "function()"? Can a list be passed the same way like a
vector?

Is it possible to pass a reference to "aAList" into the "function()"?

Thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Tue, 13 Apr 2004 03:26:36 GMT in comp.lang.c++, "sci" <sc*@focus.com>
wrote,
How to pass address of aAlist object into a function? How to call each
member inside "function()"? Can a list be passed the same way like a
vector?


Often the handy way to pass complex objects is by reference, using the
'&' as below. Then refer to the argument inside the function just as
you would any object of that type. You can do that with std::vector,
std::list, etc. but of course you cannot pass one where the other is
expected.

void function(vector<A> & vec)
{
cout << vec.size();
}

int main()
{
vector<A> aAList;
function(aAList);
return 0;
}

Remember, main() always returns an int.

Jul 22 '05 #2

P: n/a
sci wrote:
class A;

void function(vector<A>* l)
{
...
}

main(){
vector<A> aAList;
function(&aAList[0]);
}

How to pass address of aAlist object into a function?
Should be:

int main()
{
vector<A> aAList;
function(&aAList);
return 0;
}

&aAList[0] returns the address of the first element in aAList, not the
address of the vector.

Otherwise, you got it right, pretty much. If you don't intend to change
the vector in "function", you should use "const vector<A>*" instead of
"vector<A>*".
How to call each member inside "function()"?
To call the vector's functions, you have options:
(*l).size()
//or
l->size()

To access the elements of the vector you have the similar options. To
access element i:
(*l)[i]
//or
l->at(i)

You can also use iterators:
vector<A>::const_iterator begin = l->begin();
vector<A>::const_iterator end = l->end();
do_something(begin, end);
Can a list be passed the same way like a vector?
Yes:

list<A> aAList;
function(&aAList);

void function(const list<A>* l)
{
//...
}

Or, if you want to change the list:

void function(list<A>* l)
{
//...
}
Is it possible to pass a reference to "aAList" into the "function()"?


Yes, and it is much preferred actually. You should avoid using pointers
unless you have to:

class A;

void function(const vector<A>& l)
{
//...
}

int main()
{
vector<A> aAList;
function(aAList);
return 0;
}

Inside function, you don't need any fancy dereferencing:
l.size()
l[i]
l.at(i)
vector<A>::const_iterator begin = l.begin();
vector<A>::const_iterator end = l.end();
do_something(begin, end);

But more importantly, you don't have to worry about null pointers. Once
again, if you have to change the vector in function, don't use const:

void function(vector<A>& l)
{
//...
}

mark

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.