试图在编码问题中理解此结构而迷失
Lost in trying to understand this Struct in coding problem
我有一个小的编码问题,其中涉及在被0的矩阵包围的正方形矩阵中找到最大的子值。
我了解计算最大子值问题的算法。但是,我很难理解源代码。结构正在创建正方形,我对结构中的语句和变量(例如size_
,data_
等(感到困惑。
此外,使用struct Square
提供了main()
功能中的Square s
,因此我必须了解该结构如何工作,然后才能与Square s
一起使用以找到方形矩阵中最大的子值。
我也不确定如何以其格式读取矩阵Square s
的值。我想改用2D数组表示。
要了解outRow
和outCol
和outSize
在findSquare()
中的意思是什么,我尝试使用cout
将它们打印出来,但是我分别获得了值4197501、0和0的值。我不知道为什么或如何。
/******************************************************************************
Given a square matrix with values representing pixel colors black (0) and
white (1), implement a function that finds the largest subsquare with all four
borders filled with black pixels. The function should compute three values:
row and column of the top left corner of the subsquare, and its side length.
If no such subsquare exists, the return side length should be set to 0.
Note:
- Do not change 'findSquare' function signature.
******************************************************************************/
#include <cmath>
#include <memory>
#include <iostream>
using namespace std;
// Square is defined as follows:
struct Square
{
public:
explicit Square(size_t size) : size_(size), data_(new int[size * size])
{ }
Square(std::initializer_list<int> init) : size_(init.size()), data_()
{
size_ = std::sqrt(init.size());
if (init.size() != (size_ * size_))
throw std::invalid_argument("Not enough initializer elements to complete the square");
data_.reset(new int[size_ * size_]);
std::move(std::begin(init), std::end(init), &data_[0]);
}
int& operator()(size_t row, size_t col)
{
if (row >= size_ || col >= size_)
throw std::out_of_range("OOB");
return data_[size_ * row + col];
}
const int& operator()(size_t row, size_t col) const
{
if (row >= size_ || col >= size_)
throw std::out_of_range("OOB");
return data_[size_ * row + col];
}
size_t Size() const { return size_; }
private:
size_t size_;
std::unique_ptr<int[]> data_;
};
void findSquare(const Square& square, size_t& outRow, size_t& outCol, size_t& outSize)
{
// your code here
// My code. Trying to understand what outRow, outCol, and outSize represent
cout << "This is row: " << outRow << 'n';
cout << "This is col: " << outCol << 'n';
cout << "This is size: " << outSize << 'n';
}
int main()
{
Square s
{
1, 0, 0, 0,
1, 0, 0, 0,
1, 0, 0, 0,
1, 1, 1, 0
};
size_t row, col, size;
findSquare(s, row, col, size);
}
outRow
outCol
和 outSize
是输出参数。当调用函数时,它们没有意义(这就是为什么您获得垃圾值的原因(。当您计算结果时,他们在那里供您分配。
outSize = size of largest subsquare
outRow = row of largest subsquare
outCol = column of largest subsquare
至于Square
的实现只是一个方形2D矩阵,您不必了解它的工作原理,而只是了解如何使用它。它使用operator()
进行索引和大小的方法Size
。因此,您可以写
for (size_t i = 0; i < square.size(); ++i)
{
for (size_t j = 0; j < square.size(); ++j)
{
cout << square(i, j);
}
cout << 'n';
}
打印出Square
和
size_t size;
cin >> size;
Square square(size);
for (size_t i = 0; i < size; ++i)
{
for (size_t j = 0; j < size; ++j)
{
cin >> square(i, j);
}
}
在Square
中阅读。
相关文章:
- 如何循环打印顶点结构
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 预处理器:插入结构名称中的前一个行号
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 孤立代码块在结构中引发异常
- 有什么方法可以遍历结构吗
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 无法将结构注册为增强几何体3D点
- 多成员Constexpr结构初始化
- C++将文本文件中的数据读取到结构数组中
- 如何重构类层次结构以避免菱形问题
- 如何在C++中序列化结构数据
- std::vector的包装器,使数组的结构看起来像结构的数组
- 没有为自己的结构调用列表推回方法
- 奇怪的结构&GCC&clang(void*返回类型)
- 在 c++ 中拥有一组结构的正确方法是什么?
- 试图在编码问题中理解此结构而迷失
- 如何不迷失在执行'<'对于多变量结构