【问题标题】:Copy a section of bi dimensional onto itself将二维的一部分复制到自身上
【发布时间】:2016-02-27 03:41:44
【问题描述】:

我有一个二维布尔(整数)数组,如下所示:

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

假设我想复制前 2 by 2 部分

0 1
0 0

在剩下的三个象限上,最后看起来像下面这样:

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

我想使用memcpy,因为我相信这是最快的方法。在这种情况下我该如何使用它?甚至可以复制二维内容吗?

如果不是,那么我唯一的改变是拥有一个按 N * N 索引的一维数组?

【问题讨论】:

  • 好吧,对于这种情况(也许仅适用于此),将0复制到第一列和第三列,将1复制到第二列和第四列。对于N*N,做奇偶列。
  • 那行得通,但我需要一个更通用的解决方案。我将编辑问题以使其更清楚。
  • 您使用哪种语言?选择一个

标签: c++ c multidimensional-array memcpy


【解决方案1】:

多维数组在内存中的布局将与一个更长的数组相同。在这两种情况下,memcpy 在这里都帮不了你(反正打一个电话)。

01010000
01010000
00000000
00000000

虽然在抽象布局中,看起来你可以复制那个正方形,实际上它看起来像:

01000000010000000000000000000000
^^^^^^^^^^^^

您还必须在第一行包含所有值。

另外,memcpy 只能复制源数组的长度。您无法通过单个 memcpy 调用将 4 个字节复制到 16 个字节。

编辑:在我写这篇文章时,对问题进行了编辑以更改值。但是,我认为原始设置可以更好地说明我的观点。只有当模式正确时才会巧合。

【讨论】:

    【解决方案2】:

    记住

    过早的优化是万恶之源

    编写可读代码,当应用程序不够有效时,然后对其进行分析,您会看到瓶颈在哪里,但在这一点上,我几乎可以肯定您没有使用 memcpy 不会产生太大影响就像你想的那样。

    但是..

    您标记了 C++,所以这是我的简单可编译 C++ 示例实现。 它将子矩阵复制到矩阵的末尾(不是之前,所以如果你将传递该子矩阵的起点,例如 2,3 它不会将它复制到 0,0)

        #include <iostream>
        #include <iterator>
        #include <algorithm>
    
        using namespace std;
    
        void copy_submatrix( size_t n, 
                             size_t m, 
                             const pair<size_t, size_t> &from, 
                             const pair<size_t, size_t> &to, 
                             vector<vector<int>> &oryginal_matrix )
        {
            auto begin_y = oryginal_matrix.begin() + from.first;
            auto begin_to_y = oryginal_matrix.begin() + to.first;
            auto end_y = begin_y + m;
    
    
            for( ; begin_y != end_y; ++begin_y, ++begin_to_y )
            {
                auto begin_x = (*begin_y).begin() + from.second;
                auto begin_to_x = (*begin_to_y).begin() + to.second;
    
                copy( begin_x, begin_x + n, begin_to_x );
            }
        }
    
        void process_matrix( vector<vector<int>> &matrix, 
                             size_t submatrix_n, 
                             size_t submatrix_m )
        {
            for( size_t y = 0; y < matrix.size(); y += submatrix_m )
                for( size_t x = 0; x < matrix.size(); x += submatrix_n )
                    copy_submatrix( submatrix_n,
                                    submatrix_m,
                                    { 0, 0 },
                                    { y, x },
                                    matrix );
        }
    
        void print( const vector<vector<int>> &matrix )
        {
            for( const auto &v : matrix )
            {
                copy( v.begin(),
                      v.end(),
                      ostream_iterator<int>( cout, " " ) );
    
                cout << "\n";
            }
        }
    
        int main() {
            vector<vector<int>> matrix{ 
                { 0, 1, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0 }, 
                { 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0 }
            };
    
    
            cout<<"before: \n";
            print( matrix );
    
            process_matrix( matrix, 2, 3 );
    
            cout << "after: \n";
            print( matrix );
    
            return 0;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-17
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多