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' ;
}
}
|