push_back() value into vector of vectors

How to push_back() value into vector of vectors?

this gets compile error on line 16:
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
#include <vector>
#include <iostream>

int main()
{
    std::vector<std::vector<int> > vRows;

    for (int r=1; r<6; r++)
    {
        std::cout << "values= ";

        for (int c=1; c<3; c++)
        {
            int value = r*10 + c;
            std::cout << value << " ";
            vRows.push_back(value);      //compile error
        }
        std::cout << std::endl;
    }

    for (auto& itRow : vRows)
    {
        std::cout << "cells = ";

        for (auto& itCell : itRow)
        {
            std::cout << itCell << " ";
        }
        std::cout << std::endl;
    }
}


compile error:
1
2
3
4
$  g++ vector_2D.cpp
vector_2D.cpp: In function β€˜int main()’:
vector_2D.cpp:25:34: error: no matching function for call to β€˜std::vector<std::vector<int> >::push_back(int&)’
             vRows.push_back(value);


this works:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <vector>
#include <iostream>

int main()
{
    std::vector<std::vector<int> > vRows{ {1, 2, 3}, {4, 5, 6} };

    for (auto& itRow : vRows)
    {
        std::cout << "cells = ";

        for (auto& itCell : itRow)
        {
            std::cout << itCell << " ";
        }
        std::cout << std::endl;
    }
}


output:
1
2
cells = 1 2 3 
cells = 4 5 6 
Last edited on
vRows is a vector<vector<int>> you need to push a vector<int> not an int into this vector.

Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
    for (int r=1; r<6; r++)
    {
        std::cout << "values= ";
        vector<int> temp;
        for (int c=1; c<3; c++)
        {
            int value = r*10 + c;
            std::cout << value << " ";
            temp.push_back(value);     // Push the int into the temporary vector<int>
        }
        std::cout << std::endl;
        vRows.push_back(temp);
    }


Thanks jlb, that worked:
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 <vector>
#include <iostream>

int main()
{
    std::vector<std::vector<int> > vRows;

    for (int r=1; r<6; r++)
    {
        std::cout << "values= ";
        std::vector<int> tempCells;

        for (int c=1; c<3; c++)
        {
            int value = r*10 + c;
            std::cout << value << " ";
            tempCells.push_back(value); // Push the int into the temporary vector<int>
        }
        std::cout << std::endl;
        vRows.push_back(tempCells);
    }
    std::cout << std::endl;

    for (auto& itRow : vRows)
    {
        std::cout << "cells = ";

        for (auto& itCell : itRow)
        {
            std::cout << itCell << " ";
        }
        std::cout << std::endl;
    }
}


output:
1
2
3
4
5
6
7
8
9
10
11
values= 11 12 
values= 21 22 
values= 31 32 
values= 41 42 
values= 51 52 

cells = 11 12 
cells = 21 22 
cells = 31 32 
cells = 41 42 
cells = 51 52 
By the way you really should start getting used to the fact that C++ uses zero based arrays.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    for (int r = 0; r < 5; r++)
    {
        std::cout << "values= ";
        std::vector<int> tempCells;

        for (int c = 0; c < 2; c++)
        {
            int value = r * 10 + c;
            std::cout << value << " ";
            tempCells.push_back(value); // Push the int into the temporary vector<int>
        }
        std::cout << std::endl;
        vRows.push_back(tempCells);
    }


Vectors or Arrays in C++ start at zero and stop at size - 1, element 0 is the first element, not element 1.

Last edited on
Yes I know; I started example data with "1" to avoid missing leading zeros, so that output columns are aligned and pretty.

Thanks for your concern jlb :-)
Topic archived. No new replies allowed.