473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 2216
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.