make rectangular a non-rectangular multidimensional vector

Hello people,

I want to write a template that receives a -not necessarily rectangular-
multidimensional vector (vector of vectors of ...), checks if it is
non-rectangular and in such a case fill the same with zeros.
I tried this one, but it isn't working. I don't know why.

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
//
template < typename T > void make_rectangular( T& ) { }

template < typename T > void make_rectangular( std::vector<T>& matrix )//, const V defaultValue )
{
	static const auto neq_size = []( const auto& a, const auto& b ) { return a.size() != b.size() ; } ;
	if( std::adjacent_find( std::begin(matrix), std::end(matrix), neq_size ) != std::end(matrix) ) { // if true, it is non rectangular and must be filled
            struct Max
            {
                Max(): max{0} { }
                void operator()(T& element)
                {
                    if ( element.size() > max )
                        max = element.size();
                }
                std::size_t max;
            };
            Max m = std::for_each(matrix.begin(), matrix.end(), Max());
			for (auto& s : matrix)
				s.resize(m.max);
			
			std::cout << "\n" << m.max << "\n";
	}

	for( const auto& v : matrix ) make_rectangular(v) ;
} // end template make_rectangular 


Test here: http://coliru.stacked-crooked.com/a/dc04b9b9610d464d

Another question, which is the best way to get the multidimensional vector filled with a value received by the template as an argument?

Thanks
Last edited on
It seems like I found here a non very orthodox solution:

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
40
41
42
43
44
45
46
//
template < typename T, typename V > void make_rectangular( std::vector<T>&, const V& ) { }

template < typename T, typename V > void make_rectangular( std::vector<std::vector<T>>& matrix, const V& defaultValue )
{
	static const auto neq_size = []( const auto& a, const auto& b ) { return a.size() != b.size() ; } ;
	if( std::adjacent_find( std::begin(matrix), std::end(matrix), neq_size ) != std::end(matrix) ) { // if true, it is non rectangular and must be filled
            struct Max
            {
                Max(): max{0} { }
                void operator()(std::vector<T>& element)
                {
                    if ( element.size() > max )
                        max = element.size();
                }
                std::size_t max;
            };
            Max m = std::for_each(matrix.begin(), matrix.end(), Max());
			for (auto& s : matrix)
				s.resize(m.max, defaultValue);
	}

	for( auto& v : matrix ) make_rectangular(v,defaultValue) ;
}

template < typename T, typename V > void make_rectangular( std::vector<std::vector<std::vector<T>>>& matrix, const V& defaultValue )
{
	static const auto neq_size = []( const auto& a, const auto& b ) { return a.size() != b.size() ; } ;
	if( std::adjacent_find( std::begin(matrix), std::end(matrix), neq_size ) != std::end(matrix) ) { // if true, it is non rectangular and must be filled
            struct Max
            {
                Max(): max{0} { }
                void operator()(std::vector<std::vector<T>>& element)
                {
                    if ( element.size() > max )
                        max = element.size();
                }
                std::size_t max;
            };
            Max m = std::for_each(matrix.begin(), matrix.end(), Max());
			for (auto& s : matrix)
				s.resize(m.max);
	}

	for( auto& v : matrix ) make_rectangular(v,defaultValue) ;
} // end template make_rectangular 


A test here: http://coliru.stacked-crooked.com/a/708262448ec440d5

I am open to suggestions because I am not really sure this is the best way to do it.

Thanks
Topic archived. No new replies allowed.