Pass vector into a function

S

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!
 
D

David Harmon

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.
 
M

Mark A. Gibbs

sci said:
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 said:
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)
//or
l->at(i)

You can also use iterators:
vector<A>::const_iterator begin = l->begin();
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,153
Latest member
NamKaufman
Top