Hi
How do you pass a vector array as an argument to a function? What's the syntax?
Thank you
This is a discussion on pass a vector as argument within the C++ Programming forums, part of the General Programming Boards category; Hi How do you pass a vector array as an argument to a function? What's the syntax? Thank you...
Hi
How do you pass a vector array as an argument to a function? What's the syntax?
Thank you
>How do you pass a vector array as an argument to a function?
A vector, or an array of vectors? I find your choice of words ambiguous. Here's both:
But if it's the latter, you really should be using a vector of vectors instead of an array of vectors.Code:void a ( vector<int>& v ); void b ( vector<int> av[] ); ... int main() { vector<int> v; vector<int> av[10]; a ( v ); b ( av ); }
My best code is written with the delete key.
Hi
Sorry, I'm new to this, I mean't just a vector. I'm using a vector instead of an array because I need to get the size, and I don't know how to get the size of an array.
>and I don't know how to get the size of an array
Save it as a variable and pass it into your function:
But using vectors is better anyway, so you stumbled on the superior solution for a different reason.Code:void f ( int array[], int size ); int main() { const int size = 10; int array[size]; f ( array, size ); }Anyway, in my previous post, look at how I declared and called a(), that should show you how to pass a vector to a function. If you want the vector to be read-only, you can make it const:
Code:void a ( const vector<int>& v );
My best code is written with the delete key.