2D STL Vector pass by value

I'm reading through an input file that is formatted as: f 123/456/789 987/654/321 741/852/963
I've parsed it so that the "temp" variable stores each 3 digit integer one at a time and stores all 9 of them to a vector V1 using push_back. The next step is that the 2D vector "vecf" stores V1 using vecf.push_back(V1). When I read in 100 sets of these 9 numbers (all unique), they are being passed as a reference to vecf, so essentially I have 100x9 vector of the same 9 numbers. I'm not sure how to get around this. Any suggestions?

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
vector<vector<unsigned> > vecf;
void loadInput()
{
    char delimiter;
    //counter variable to handle delimiter locations (2 delimiters per set of 3 numbers)
    unsigned z=0;
    float u,v,w;
    //temporary storage for appends
    unsigned temp;
    vector<unsigned> V1;
    //string storage for getline and parsing
    string buffer;
    string s;

    while(getline(cin, buffer)){
        stringstream ss(buffer); // create ss object
        ss >> s; // store first entry (always a character identifier (v= vertex, vn = normal, f = face)
        if (s == "f"){
                while(ss >> temp){ // For an arbitrary number of faces (goes until end of line)
                V1.push_back(temp); // append all face data to vector
                    if(z!=2){ // z cycles between [0,1,2], on 3rd cycle there is no delimiter. Data format : a/b/c
                        ss >> delimiter; //only parse delimiter for first two cycles
                    }
                z = (z+1)%3; // increment z
                }
                vecf.push_back(V1); // 2-D vector of face data
        }
    }
    cout << vecf[0][0] << vecf[1][0];
}


I guess another way of wording the question is: how can I pass the integers on line 20 by value instead of reference? And how can I pass the vector V1's set of values to vecf by value instead of by reference?

I'm looking for ideas on how to accomplish that goal.
Last edited on
Looks like you never clear V1. Should you be clearing it after each time you copy it onto the back of vecf? Otherwise, the first time it'll have 9 values in it, and then the next time round it'll have the same 9 values and the next 9, and then the third time round those same 18 and the next 9, and so on...
Last edited on
And all along I thought it was because push_back() takes a reference instead of a value. I did V1.clear() after the push to vecf and now my object is being plotted as a sphere instead of plotting the same triangle 760 times...

Thank you very much. Can you explain a little bit more about what is going on behind the scenes though? I thought hat since I'm doing vecf.push_back(V1) that vecf now contains a reference to V1. In my loop I'm pushing V1 to vecf many times, and on each iteration of the loop I'm changing what the values of V1 are (and thanks to your suggestion clearing it).

Why is it that at the end vecf is not just a vector full of V1 vectors, which I'd imagine all have the same value since they are being pushed by reference? I mean... I'm glad that isn't happening! One less problem I have to solve, but I don't understand perfectly why that is.
Last edited on
push_back makes a copy, a whole new instance of the object, on the end of the vector.

You assemble V1. It has nine distinct values in it. You push it onto vecf; a complete copy of V1 appears on the end of vecf.

The function named push_back takes a reference to V1, and then inside that function it reads V1 and makes a copy of V1 on the end of vecf.

The function push_back gets a reference to V1, but what goes on the end of vecf isn't that reference; it's a copy, made inside the function named push_back.
Last edited on
Topic archived. No new replies allowed.