473,804 Members | 3,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass vector into a function

sci
class A;

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

main(){
vector<A> aAList;
function(&aALis t[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 2246
On Tue, 13 Apr 2004 03:26:36 GMT in comp.lang.c++, "sci" <sc*@focus.co m>
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(&aALis t[0]);
}

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

int main()
{
vector<A> aAList;
function(&aALis t);
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>::cons t_iterator begin = l->begin();
vector<A>::cons t_iterator end = l->end();
do_something(be gin, end);
Can a list be passed the same way like a vector?
Yes:

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

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>::cons t_iterator begin = l.begin();
vector<A>::cons t_iterator end = l.end();
do_something(be gin, 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
3328
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
6
2740
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 Vectors ================================== std::vector<float> gTmpArr(8000,0); In Main, I would like to pass this vector to a function such as:
41
8344
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 reference?? Thanks, BB
23
6553
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: vector<string> tokenize(string s){
6
5655
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. char temp; std::vector<unsigned charmmessage; while (!done){
10
2282
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 beginner easies) way to do so? I read a bit about pointers on the web and in books but the examples do
1
2491
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
1955
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: class room { public: string description; room(string s,??) //problem here { s=description; }
0
10319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10303
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2978
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.