Pushing Back A Vector Into a Vector

May 13, 2016 at 6:54pm
I am writing a program that should append a vector w to a vector v. But I get a compilation error at "v.push_back(w);". I would appreciate the feedback. Thank you!

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

using namespace std;

void appendVector(vector<int> & v, const vector<int> & w);

int main()
{
    vector<int> v1{};
    vector<int> v2{1,2,3};
    append(v1,v2);
    assert(v1[0]==0);
    assert(v1[1] == 1);
    assert(v1[2] == 2);

    vector<int> v3{79,81,90};
    vector<int> v4{55,66,77};
    append(v3,v4);
    assert(v3[0] == 79);
    assert(v3[1] == 81);
    assert(v3[2] == 90);
    assert(v3[3] == 55);
    assert(v3[4] == 66);
    assert(v3[5] == 77);
   
    cout << "All tests have passed successfully." << endl;
}

void appendVector(vector<int> & v, const vector<int> & w)
{
    v.push_back(w); 
}
Last edited on May 13, 2016 at 6:55pm
May 13, 2016 at 7:06pm
line 13, 20: You're calling append. There is no global append function. Did you mean to call appendVector()?

line 33: v is a vector of ints, so push_back is expecting an int.
Use vector::insert to append one vector to another (form 3).
http://www.cplusplus.com/reference/vector/vector/insert/

May 13, 2016 at 8:50pm
Thanks!

Re lines 13 & 20: fixed.

Re line 33: I got this " v.insert(v.end(),w.begin(),w.(end));" but it doesn't work. I assume the first argument or parameter in this function specifies when to begin the appendation, the second argument specifies the first element to be appended to the vector, and the third argument specifies the last element to be appended to the vector.

In any case, I am trying to do this without using the vector insert function, but by using push_backs.
May 13, 2016 at 8:59pm
I think I got it. Here is my code without using the standard vector insert function:

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

#include <iostream>
#include <cassert>
#include <vector>

using namespace std;

void appendVector(vector<int> & v, const vector<int> & w);

int main()
{
    vector<int> v1{0};
    vector<int> v2{1,2,3};
    appendVector(v1,v2);
    assert(v1[0]==0);
    assert(v1[1] == 1);
    assert(v1[2] == 2);

    vector<int> v3{79,81,90};
    vector<int> v4{55,66,77};
    appendVector(v3,v4);
    assert(v3[0] == 79);
    assert(v3[1] == 81);
    assert(v3[2] == 90);
    assert(v3[3] == 55);
    assert(v3[4] == 66);
    assert(v3[5] == 77);
   
    cout << "All tests have passed successfully." << endl;
}

void appendVector(vector<int> & v, const vector<int> & w)
{
    for(int i=0; i < w.size(); ++i)
    {
        v.push_back(w[i]);
    }
}


NOTE: I had to input '0' into line 11's vector initialization, otherwise the line 14 assert statement would fail. I assume that this is because the elements of empty vectors, unlike the elements of empty arrays, are not automatically set to '0'.
Last edited on May 13, 2016 at 9:02pm
Topic archived. No new replies allowed.