Question about references and vectors

Hi!

My first time posting here and hope i don't make a fool of myself! But i have a few questions about this piece of code:

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
  #include <iostream>
#include <vector>

using namespace std;

void f1(vector<double> v1){
    sort(v1.begin(), v1.end());
}

void f2(vector<double>& v2){
    sort(v2.begin(), v2.end());
}

int main() {
    vector<double> a;
    vector<double>::const_iterator i;
    
    a.push_back(1);
    a.push_back(3);
    a.push_back(2);
    a.push_back(5);
    a.push_back(4);
    
    //f1(a);
    f2(a);
    
    for(i=a.begin(); i!=a.end(); ++i){
        cout<< *i<<endl;
    }

    return 0;

}


Using my two functions f1 and f2 i get different outputs. Where f1 gives me the unsorted array and f2 sorts the array correctly. Why is this? Is it a pass by reference thing?

And one one question: While reading on the web about this some states that its bad programing to use a reference with vectors but my program compiles just fine (c++11)? Is it bad programing to write f2(vector<double>& v2)?

Thank you for your time!
Last edited on
My first time posting here and hope i don't make a fool of myself!


You're one of the very few who use code tags in their first posts, very good impression so far.

Yes, it's because of the pass by reference thing. In function 1, the vector is being copied and the operation is being performed on the copied vector, so when you print out the vector in main, nothing has changed since you didnt do anything with the original vector, but rather the copy.

In the second function, since you pass it by reference, you're passing the original vector and the operations in there take place on it, so when you print it out in main, the changes done to the vector will be made.



And one one question: While reading on the web about this some states that its bad programing to use a reference with vectors but my program compiles just fine (c++11)? Is it bad programing to write f2(vector<double>& v2)?


Personally Ive never heard this, I'm not an expert though so someone else is going to have to shed some extra light. But passing by reference is very useful and indeed needed, especially when there are big data objects, it can be very expensive making copies, same thing regarding returning by reference.

For example, I'm making a 3D Application, and I have models loaded in. Whenever I want to use one of the models, if my function that returns the model does not return it by reference but rather by value, my applications fps drops to about 60. When I later changed it to return by reference, the fps went up to something like 400.
Last edited on
Excellent explanation! Thanks! On that "f2(vector<double>& v2)" question i must have read wrong... I was reading about why you can't make vectors of references like vector<int &> v1!

Thanks again
There are situations when you might want to pass a vector by value but passing vectors by reference is usually recommended because it can avoid unnecessary copying.

If you want to modify the vector that is passed to the function you should do like you did in your f2 function. Only danger I see with this is that it can be confusing when functions modify arguments, but if you give the function a more descriptive name, such as "sortVector", it becomes more obvious that the function will actually modify the vector.

If the function does not modify the vector you should mark the reference as const. This makes it clear that when you call the function the vector will not be modified. It also prevents you from modifying the vector by accident.

1
2
3
4
void f3(const vector<double>& v3)
{
	// Here you can use, but not modify, v3.
}
Last edited on
Topic archived. No new replies allowed.