how to pass a vector as a parameter to a function?

as the title states
as a reference or const reference (depending on your intent to modify it or not inside the function)
here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void f1(vector<int> & v)
{
     //do what you want here...
}

void f2(const vector<int> & v)
{
     //do what you want here...
}

int main()
{
     vector<int> v1;
     const vector<int> v2;

     f1(v1);
     f2(v2);

     return 0;
}

now, if you want the function you use to be generic (that is handle all kinds of vectors) you'll have to use templates
Last edited on
so vectors have to be passed by reference only? i can't do it normally? also what's the diff between passing it as a const vector ref and a normal vector ref? sorry im a noob
so vectors have to be passed by reference only? i can't do it normally?

of course you can pass them normally (that is pass them by value). but when you pass objects by value what happens is that a new object is created and then the object you pass is copied to the new object. this, for standard types (char, int, float etc) is ok, because standard types are small in size (char is one byte, short is 2, int is 2 or 4, long is 4 etc...). a vector however is not that small. Try doing this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <vector>
using namespace std;

int main()
{
     vector<int> v;
     cout << sizeof(v) << endl;

     system("pause");
     return 0;
}


in my pc I get 12 as an output. that's 12 bytes. so instead of passing by value, I pass it by reference which has a cost of 4 bytes (the size of the variable holding the address of my vector)

also what's the diff between passing it as a const vector ref and a normal vector ref?

using the const keyword will inform your compiler that you don't want to change the object passed. If you try to do it accidentally your compiler will generate an error.

sorry im a noob

one is what he thinks himself being

for more info about passing arguments in functions check this:
http://www.cplusplus.com/doc/tutorial/functions2/
thanks alot man. so using the const keyword is a way to pass by ref without changing the value of the vector being passed correct? so isn't that the same as passing a vector normally? since when u pass it by ref with const, it does not change the vector. and when you pass it normally, it just makes a copy of the vector and operates on that correct? so the original vector is never changed.
Absolutely! The only difference is that passing a vector by value (what you refer to as "normally") costs more than passing it by a const reference. So, to sum up, if you don't want to modify an object, if its size is <=4 pass it by value, else pass it by a const reference. And if you want to modify it pass it by a (non-const) reference.
@master roshi...i don't want to modify the original, however, i want be able to use the data from the original vector and modify that data but not the vector itself. i passed it by value and that works however, when i try to pass by const ref and modify the data, i get an error like you said.

if i pass a vector by const ref but i can't modify it's data? then what's the point? is there a way to solve this without passing it by value?
Pass it by reference, not const reference, like m4ster r0shi wrote in an earlier post:

1
2
3
4
void f1(vector<int> & v)
{
     //do what you want here...
}
@sammy, yea but passing by ref changes the original vector, i do not want to change the original vector. i just want to operate on the data it contains. i can pass by value and make a copy of the vector and work on the copy and the original would not be changed but i do not want to do that. whats another way of doing this? roshi said that that passing by const ref does not change the original vector, however, you can't operate on the data cause like he said, any attempts to change the object passed would produce an error.

if so, then what is const ref useful for?
passing by reference itself doesn't change the original vector. passing by reference allows you to change the vector inside your function (which is equivalent to modifying the data it contains)
if you don't want to modify the original vector but want to change the data maybe the solution to your problem is creating a new copy of the vector and work with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

void modify(vector<int> & v)
{
    int size=v.size();
    for (int i=0; i<size; i++)
    {
        v[i]++;
    }
}

void print(const vector<int> & v)
{
    int size=v.size();
    for (int i=0; i<size; i++)
    {
        cout << v[i] << ' ';
    }
    cout << endl;
}

int main()
{
    vector<int> v1;
    v1.resize(5);
    
    for (int i=0; i<5; i++)
    {
        v1[i]=i;
    }
    
    cout << "v1 is: ";
    print(v1);
    
    vector<int> v2;
    v2=v1;
    
    modify(v2);
    
    cout << "v2 is: ";
    print(v2);
    
    cout << "v1 is: ";
    print(v1);
        
    system("pause");
    return 0;
}
Last edited on
Generally (such as in the STL), vectors and other containers are passed as a pair of iterators to the range targeted, rather than the whole container; however I wouldn't say that that's a great amount different from passing it by ref. (The main advantage is more user control over the affected range of elements, afaik.) If you are trying to manipulate a copy of the vector just pass it normally, as an argument; a copy of it will be created (although this is rather memory inefficient).
I can imagine that const ref would come in handy if you had a real memory hog of an object that you needed to handle without modifying the original. Passing a copy would be a ram disaster. Of course, it's quite possible that such a massive variable would be a god object, but that's another matter.
Topic archived. No new replies allowed.