Vector size

Hi, I'm beginner in c++. I would like to ask. How can I standardize the size of each row by putting zero at the back of row.

Then the solution should be:
11 12 13 14 15
16 17 18 19 0
20 21 22 23 24
25 26 27 28 0
29 30 31 32 33

Any advice?. Thanks for help me.

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

using namespace std;

int main()
{
    vector<vector<int>> vec { { 11, 12, 13, 14, 15 }, 
                              { 16, 17, 18, 19 },
                              { 20, 21, 22, 23, 24 },
                              { 25, 26, 27, 28 },
                              { 29, 30, 31, 32, 33 } };
                             
    for (int i = 0; i < vec.size(); i++)
    {
        for (int j = 0; j < vec[i].size(); j++)
        {
            cout << vec[i][j] << "  ";
        }
        cout << endl;
    }
    cout << endl;
    return 0;
}
Do you want to add a final 0 in the shortest rows in the std::vector or do you just want to output a 0 if the row is shorter then the others?
I want to add a final 0 in the shortest row in the std::vector.
In this example, it is row 2 and row 4. So that vector will have equal size (row = 5 and col = 5). It is impossible to do it ?l
Last edited on
This vector<vector<int>> is not a matrix, the "outer" vector is a collection of "inner" vectors -- each of them with its own length. So if there should be a 0 at the rhs do the same as you would with a zero at the lhs -- define it. Or define a minumum length for the inner vectors.
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
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    vector<vector<int>> vec { { 11, 12, 13, 14 }, 
                              { 16, 17, 18, 19, 0 },
                              { 0 },
                              { 25, 26, 27, 28 },
                              { 29, 30, 31, 32, 33 } };
                             
    for (uint i = 0; i < vec.size(); i++)
    {
        for (int j = 0; j < 5; j++)
        {
            cout << vec[i][j] << "  ";
        }
        cout << endl;
    }
    cout << endl;
    return 0;
}

Question: why can't this { 0 } vector not be defined like this: { }?
Oh i see. I just need define the length of the column, then will be automatically fill with 0 at back.

MikeStgt, for your question. Sorry I don't know. I still new to c++.

Again, thanks for your help :)
I had try to change based on yours. I got debug assertion failed.
Expression: vector subscript out of range.
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
#include <iostream>
#include <vector>
#include <iomanip>

void make_same_size( std::vector< std::vector<int> >& vec2d )
{
    if( vec2d.size() > 1 )
    {
        // find the size of the longest row
        std::size_t max_sz = 0 ;
        // auto: http://www.stroustrup.com/C++11FAQ.html#auto
        // range-based loop: http://www.stroustrup.com/C++11FAQ.html#for
        for( const auto& row : vec2d ) if( row.size() > max_sz ) max_sz = row.size() ;

        // resize all rows to max_sz
        for( auto& row : vec2d ) row.resize(max_sz) ;
    }
}

int main()
{
    std::vector< std::vector<int> > vec { { 11, 12, 13, 14, 15 },
                                          { 16, 17, 18, 19 },
                                          { 20, 21, 22, 23, 24, 25, 26 },
                                          { 25, 26, 27, 28 },
                                          { 29, 30, 31, 32, 33 },
                                          { 34, 35 },
                                          { 36, 37, 38 },
                                        };

     make_same_size(vec) ;

     for( const auto& row : vec )
     {
         for( int v : row ) std::cout << std::setw(3) << v ;
         std::cout << '\n' ;
     }

}

http://coliru.stacked-crooked.com/a/0d9061ca71b1f21f
Last edited on
I still new to c++

Same holds true for me. As such I may not realy help in sense of advise, I am learning too and share my smattering.

In the tutorials look for 'Multidimensional arrays' here:
http://www.cplusplus.com/doc/tutorial/arrays/
Instead of "vector of vectors" they stipulate
Multidimensional arrays can be described as "arrays of arrays".
MikeStgt, its okay. I really appreciate your effort to help me. Same here, still learns. Still far to go. A lot to learn. Btw, good luck!

To JLBorges, thanks for your help. I will try to understand your given codes.

Thank you. Have a nice day :)
Topic archived. No new replies allowed.