468,738 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

Pass vector into a function

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
2 1979
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
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.

Similar topics

2 posts views Thread by sci | last post: by
6 posts views Thread by Dennis | last post: by
41 posts views Thread by Berk Birand | last post: by
23 posts views Thread by Sanjay Kumar | last post: by
10 posts views Thread by Bernhard Reinhardt | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.