472,954 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

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 2192
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
6
by: Dennis | last post by:
In my program I have defined a global vector with //=======prototypes section ==================== float select(const int k, std::vector<float> &myArr, int iStart, int iEnd); ..... //====Global...
41
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
6
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. ...
10
by: Bernhard Reinhardt | last post by:
Hi, I read data from a file into a 4 dimensional array. The data has a size of 5MB (could later be up to 500MB). I want to pass the matrix to subroutines. What is the cleverst (and for a...
1
by: Lambda | last post by:
I defined a class: class inverted_index { private: std::map<std::string, std::vector<size_t index; public: std::vector<size_tintersect(const std::vector<std::string>&); };
6
by: Poke386 | last post by:
I'm in the process of making an text based-rpg in C++. Its just a little project so I can learn some object-oriented programming, nothing serious. My problem is that I've created a class like so: ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.