Want to create padded 2 dimensional vector in c++ style

I was trying to create padded 2 dimensional vector in c++ style, like using copy or copy_n function in algorithms library, couldn't think of proper code for it.

The problem is this. You get a lock and key of 2 dimensional vector. You test if your key fits the lock.

For this purpose, I wanted to create a new lock that has 0 padded one.

i.e.)

if the lock looks like

0 1 0
1 1 1
0 0 1

and key like this

1 1
0 0

I wanted to make padded lock in

0 0 0 0 0
0 0 1 0 0
0 1 1 1 0
0 0 0 1 0
0 0 0 0 0

But ended up using c style code like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef vector<int> vi;

vector<vi>* blank_filled_lock(const int key_length, const int lock_length, const vector<vi>& lock) {
    auto result_length = 2 * (key_length - 1) + lock_length;
    vector<vi>* result = new vector<vi>(result_length, vi(result_length, 0));
    for (auto row = 0; row < lock_length; ++row)
    {
        for (auto col = 0; col < lock_length; ++col)
        {
            (*result)[row + key_length - 1][col + key_length - 1] =lock[row][col];
        }
    }

    return result;
}


If there's any idea I can get, it would be of big help. thank you.
Something like this, perhaps:

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
47
48
49
50
#include <iostream>
#include <vector>
#include <algorithm>

namespace lock_utils
{
    using lock_type = std::vector< std::vector<int> > ;

    lock_type padded_lock( const lock_type& lock, std::size_t padded_size )
    {
        padded_size = std::max( padded_size, lock.size() ) ;

        lock_type padded( padded_size, std::vector<int>(padded_size) ) ;

        auto nrow = ( padded_size - lock.size() ) / 2 ;

        for( const auto& row : lock )
        {
            const auto skipped_cols = padded_size > row.size() ? ( padded_size - row.size() ) / 2 : 0 ;
            std::copy( std::begin(row), std::end(row), padded[nrow++].begin() + skipped_cols ) ;
        }

        return padded ;
    }

    std::ostream& operator<< ( std::ostream& stm, const lock_type& lock )
    {
        for( const auto& row : lock )
        {
            for( int v : row ) stm << bool(v) << ' ' ;
            stm << '\n' ;
        }
        return stm ;
    }
}

int main()
{
    using namespace lock_utils ;

    const lock_type lock { { 0, 1, 0 }, { 1, 1, 1 }, { 0, 0, 1 } } ;

    std::cout << "lock:\n-----\n" << lock << '\n' ;

    for( std::size_t sz : { 3, 5, 6, 8, 13, 16 } )
    {
        std::cout << "\npadded lock (" << sz << 'x' << sz
                  << "):\n------------------\n" << padded_lock( lock, sz ) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/1cb61926ae5b03d2
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;

ostream & operator << ( ostream & strm, const vector<string> &M )
{
   for ( string s : M ) strm << s << '\n';
   return strm;
}

vector<string> padLock( vector<string> lock, int pad )
{
   int N = lock[0].size() + 2 * pad;
   for ( string &s : lock ) s = string( pad, '0' ) + s + string( pad, '0' );
   lock.insert( lock.begin(), pad, string( N, '0' ) );
   lock.insert( lock.end()  , pad, string( N, '0' ) );
   return lock;
}

int main()
{
   vector<string> lock = { "010", "111", "001" };
   cout << lock << '\n';
   lock = padLock( lock, 1 );
   cout << lock << '\n';
}


010
111
001

00000
00100
01110
00010
00000
Topic archived. No new replies allowed.