C++ Help... passing vector to function?

Wuzup101

Platinum Member
Feb 20, 2002
2,334
37
91
Hey guys, I'm taking an intro level C++ class and I'm having a bit of a problem. There is some extra credit on a recent assignment that requires me to pass a vector to a function and have the function do the data inputting (prompting the user and such). I know how to do this for an array; however, I'm not sure how similar this process is when dealing with a vector. It also happens that my school (Penn State) has casterated my book by making an exculsive PSU copy that ommits the more advanced chapters on vectors. Anyone want to give me a clue or recomend me to a good C++ site? I have the program working perfectly without the function (this is the non extra-credit version).
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Which part are you unclear about: how to pass a vector to a function or how to add data to a vector?
 
Feb 28, 2004
72
0
0
Is this an STL vector you're talking about? Sorry, I've not done much STL (and I'm drunk as usual), but have you tried passing the vector object into the function by reference?

e.g.

// these things are dynamic - they grow as required.
typedef vector{int} intVector; //replace {} with <> - forum confuses <> with html tag!!!

say you have a function prototype: getData(intVector &inputVector);

and the function body:

// get data from the user and store it in the vector.
void getData(intVector &inputVector)
{
int anInt = get an integer from the user...

inputVector.push_back(anInt); // this method adds anInt to the end

intputVector.push_back(another integer);
}

In your main program:

whatever
{
...
// say you want a local instance of an intVector
intVector theInputVector;

// call the above function
getData(theInputVector);

// i think you can now use the [] operator to access elements in theInputVector e.g.
int number = theInputVector[0] * theInputVector[1]; // not sure, will have to try it!
}

Sorry if this is too simplistic/patronising I just based this on stuff I found in the MDSN library. If you don't have it on CD, try msdn.microsoft.com. Also www.codeguru.com is pretty good, as is www.programmersheaven.com.

 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Yeah, you probably want to pass it by reference. A vector is just an object, you can pass it like anything else - but since it's possibly pretty big, you likely want to pass it by reference so you're not doing a ton of copying.

edit: I would also do it in a general way, for extra credit or at least suck up points. ;)

template<typename containerT>
void add_some_stuff(containerT&amp; container)
{
// blah blah do some stuff here
// now actually append to the container (vector in your case)
container.insert(container.end(), YOUR_THING_TO_APPEND_HERE);
}

This will work with vector, list, deque, etc.

And be wary of msdn for learning - I've seen sample code that has #include <iostream.h> and such ancient things. :)
 

Wuzup101

Platinum Member
Feb 20, 2002
2,334
37
91
Alright thanks for the help guys. I think I had the general idea; however, I must have had a syntax error or 2. I'll check it out fully tomorrow, I'm a bit tired right now and my other 6 classes need some attention :(
 

Wuzup101

Platinum Member
Feb 20, 2002
2,334
37
91
Alright after like 2 seconds of trying w/ the above advice I got it working. It seems I was neglecting the vector{int} and just using type int in the function definitions and function headings... stupid mistake... wish I would have a book to tell me how to do this stuff...