如何在c ++中的函数中传递二维向量的子向量

时间:2017-07-26 21:58:06

标签: c++

void Solution::rotate(vector<vector<int> > &A) {
   int n= A.size();
int temp[n];
for(int j=0;j<n;j++)
{
    temp[j]=A[n-1][j];
}
   for(int i=0;i<n-1;i++)
   {
       A[n-1][i]=A[n-i-1][n-1];
       A[n-i-1][n-1] = A[0][n-i-1];
       A[0][n-i-1]=A[i][0];
       A[i][0]=temp[i];
       //A[i+1][0]=A[n-1][i+1];
  }
}

我想再次调用该函数并传递原始数组的子数组,其起始点为数组的(1,1),端点为数组的(n-2,n-2)。 是否有可能在不创建新向量的情况下完成它,我的意思是只是将一些指针传递给函数?

1 个答案:

答案 0 :(得分:0)

如果您只想查看子矩阵或编辑有界区域,请创建一个查看器。查看器不会做任何事情,只会限制您可以查看的矩阵区域。没有复制和使用的额外内存。例如,如果你有一个巨大的图片,并且想要查看和操作8x8元素区域,那就太棒了。

如果您需要给定视图的新矩阵,请教导查看者基于视图创建新矩阵。在这种情况下涉及复制,但是当你想要复制时,很难避免复制。

为了证明,首先我要稍微走出人迹罕至的地方。如果你想要快速(谁不想要快?)请不要使用vector<vector>。保证vector在内存中是连续的,但是当你有向量的向量保证会离开窗口时,这会导致空间局部性差,并且通常会导致缓存使用不佳。

这是一个简单的Matrix类的例子,它更易于使用,并且只是一个内存块,所以它往往更加缓存友好。

// wrapping class for 2D matrixes
class Matrix
{
private:
    size_t rows, columns; // large, unsigned datatype. Don't want negative
                          // indices, so why allow them?
    std::vector<int> matrix; // 1D vector. Simple and easy to handle.
                             // also often much faster than vector of vectors
                             // due to improved spatial locality helping
                             // predictability of data access
public:
    // build zero-filled Matrix
    Matrix(size_t numrows, size_t numcols) :
            rows(numrows), columns(numcols), matrix(rows * columns)
    {
    }

    // 2D to 1D mapping accessor
    int & operator()(size_t row, size_t column)
    {
        // check bounds here
        return matrix[row * columns + column];
    }

    // 2D to 1D mapping accessor for constant Matrix
    int operator()(size_t row, size_t column) const
    {
        // check bounds here
        return matrix[row * columns + column];
    }

    // dimension accessors
    size_t getRows() const
    {
        return rows;
    }
    size_t getColumns() const
    {
        return columns;
    }
};

现在我们有一个更快,包含得更好的Matrix类,我们可以创建一个非常简单的MatrixView类。

class MatrixView
{
    size_t mStartRow;       // view offset in row
    size_t mStartColumn;    // view offset in column
    size_t mRows;           // number of viewed rows
    size_t mColumns;        // number of viewed columns
    Matrix & mMat;          // viewed Matrix

public:
    // using start and endpoints in this constructor. A more ideologically correct 
    // constructor would behave the same as the standard library and take offset 
    // and length as parameters.
    MatrixView(size_t startrow,
               size_t startcolumn,
               size_t endrow, 
               size_t endcolumn,
               Matrix & mat):
                   mStartRow(startrow),
                   mStartColumn(startcolumn),
                   mRows(endrow - startrow),
                   mColumns(endcolumn - startcolumn),
                   mMat(mat)
    {
        //ensure dimensions make sense
        if (startrow > endrow ||
            startcolumn > endcolumn ||
            mRows > mat.getRows() ||
            mColumns > mat.getColumns())
        {
            throw std::runtime_error("Bad MatrixView dimensions");
        }
    }
    int & operator()(size_t row, size_t column)
    {
        // check bounds here if you want to
        // look at the source matrix plus offsets
        return mMat(row+mStartRow, column+mStartColumn);
    }

    // 2D to 1D mapping accessor for constant Matrix
    int operator()(size_t row, size_t column) const
    {
        // check bounds here if you want to
        return mMat(row+mStartRow, column+mStartColumn);
    }

    // dimension accessors
    size_t getRows() const
    {
        return mRows;
    }
    size_t getColumns() const
    {
        return mColumns;
    }

    // build a new Matrix based on this view
    Matrix clone()
    {
        Matrix result(mRows, mColumns);
        for (size_t row = 0; row < mRows; ++row)
        {
            for (size_t col = 0; col < mColumns; ++col)
            {
                result(row, col) = mMat(row+mStartRow, col+mStartColumn);
            }
        }
        return result;
    }
};

使用这个吸盘的一个例子:

// stream formatters
std::ostream & operator<<(std::ostream & out, const Matrix & mat)
{
    for (size_t row = 0; row < mat.getRows(); ++row)
    {
        for (size_t col = 0; col < mat.getColumns(); ++col)
        {
            std::cout << std::setw(5) << mat(row, col);
        }
        std::cout << '\n';
    }
    return out;
}
std::ostream & operator<<(std::ostream & out, const MatrixView & mat)
{
    for (size_t row = 0; row < mat.getRows(); ++row)
    {
        for (size_t col = 0; col < mat.getColumns(); ++col)
        {
            std::cout << std::setw(5) << mat(row, col);
        }
        std::cout << '\n';
    }
    return out;
}


int main()
{
    Matrix one(6, 6); // make 6x6 matrix
    int count = 0;

    // set inputs to make errors really stand out
    for (size_t row = 0; row < one.getRows(); ++row)
    {
        for (size_t col = 0; col < one.getColumns(); ++col)
        {
            one(row, col) = count++;
        }
    }

    // print initial matrix
    std::cout << one << '\n';

    // make a view of matrix that leaves off the outside.
    MatrixView view(1,1,5,5, one);

    // print the view
    std::cout << view << '\n';

    // get a clone of the view we can pass into a function 
    Matrix clone = view.clone();

    // and print the clone
    std::cout << clone << '\n';
}