int** c; c = new int* [m+1];
时间: 2024-01-17 14:01:39 浏览: 29
This code creates a dynamic 2D array of integers with m rows and 1 column, and assigns the memory address of this array to the pointer variable c.
Note that the syntax for dynamically allocating a 2D array involves creating an array of pointers to arrays, where each pointer points to a dynamically allocated array of integers.
In this case, we are creating an array of m pointers, and each pointer will point to an array of 1 integer.
To access an element in this 2D array, we would use the syntax c[i][j], where i represents the row index and j represents the column index.
相关问题
void InPut(){ cin>>n>>m>>d; w = new int*[n+1]; c = new int *[n+1]; for(int i = 1;i <= n ; i ++) { w[i] = new int [m+1]; c[i] = new int [m+1]; } leaf = NULL; for(int i = 1 ; i <= n ; i ++) for (int j = 1 ; j <= m ; j ++) cin>>c[i][j]; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>w[i][j]; }
这段代码是一个输入函数,用于读取一个二维数组的值。先输入n、m和d三个整数,表示数组的行数、列数和维度。然后动态分配一个n+1行m+1列的整型数组w和一个n+1行m+1列的整型数组c。接下来使用两个for循环分别读取数组c和数组w的值,最后将叶子节点leaf初始化为NULL。
其中,c数组表示每个节点的收益值,w数组表示每个节点的重量值。这段代码可能是用于解决背包问题或者类似的优化问题。
void C312105010501View::OnJunzhi(){ C312105010501Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (pDoc->m_pDib == NULL) { AfxMessageBox("未打开图像"); return; } int mcols = pDoc->m_pDib->m_nCols; int mrows = pDoc->m_pDib->m_nRows; unsigned char* m_pData = NULL; m_pData = pDoc->m_pDib->pImgData; unsigned char* m_pDataOldPos = m_pData; BYTE* ptmp = new BYTE[mcols * mrows]; memcpy(ptmp, m_pData, mcols * mrows); int Template[9] = { 1,1,1,1,1,1,1,1,1 }; int mask = 3; int irows, jcols; for (jcols = mask / 2; jcols < mcols - mask / 2; jcols++) { for (irows = mask / 2; irows < mrows - mask / 2; irows++) { int sum = 0, m, n; for (m = -mask / 2; m <= mask / 2; m++) { for (n = -mask / 2; n <= mask / 2; n++) { sum += ptmp[(irows + m) * mcols + jcols + n] * Template[(m + mask / 2) * mask + n + mask / 2]; } } sum = sum / 9; if (sum > 255) sum = 255; m_pData[irows * mcols + jcols] = sum; m_pData = m_pDataOldPos; } m_pData = m_pDataOldPos; } pDoc->UpdateAllViews(NULL);}仿照以上代码写一个3*5均值滤波处理和5*5均值滤波处理和7-7均值滤波处理
以下是分别实现3*5、5*5和7*7均值滤波的代码:
3*5均值滤波:
```c++
void CMyView::OnJunzhi35()
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->m_pDib == NULL)
{
AfxMessageBox("未打开图像");
return;
}
int mcols = pDoc->m_pDib->m_nCols;
int mrows = pDoc->m_pDib->m_nRows;
unsigned char* m_pData = pDoc->m_pDib->pImgData;
unsigned char* m_pDataOldPos = m_pData;
// 备份原始数据
BYTE* ptmp = new BYTE[mcols * mrows];
memcpy(ptmp, m_pData, mcols * mrows);
// 定义模板和掩码大小
int Template[15] = { 1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1 };
int maskCol = 2;
int maskRow = 1;
// 对每个像素进行处理
for (int j = maskCol; j < mcols - maskCol; j++)
{
for (int i = maskRow; i < mrows - maskRow; i++)
{
int sum = 0;
for (int m = -maskRow; m <= maskRow; m++)
{
for (int n = -maskCol; n <= maskCol; n++)
{
sum += ptmp[(i + m) * mcols + j + n] * Template[(m + maskRow) * (2 * maskCol + 1) + n + maskCol];
}
}
sum /= 15;
if (sum > 255)
{
sum = 255;
}
m_pData[i * mcols + j] = sum;
}
}
// 释放内存
delete[] ptmp;
pDoc->UpdateAllViews(NULL);
}
```
5*5均值滤波:
```c++
void CMyView::OnJunzhi55()
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->m_pDib == NULL)
{
AfxMessageBox("未打开图像");
return;
}
int mcols = pDoc->m_pDib->m_nCols;
int mrows = pDoc->m_pDib->m_nRows;
unsigned char* m_pData = pDoc->m_pDib->pImgData;
unsigned char* m_pDataOldPos = m_pData;
// 备份原始数据
BYTE* ptmp = new BYTE[mcols * mrows];
memcpy(ptmp, m_pData, mcols * mrows);
// 定义模板和掩码大小
int Template[25] = { 1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1,
1, 1, 1, 1, 1 };
int maskCol = 2;
int maskRow = 2;
// 对每个像素进行处理
for (int j = maskCol; j < mcols - maskCol; j++)
{
for (int i = maskRow; i < mrows - maskRow; i++)
{
int sum = 0;
for (int m = -maskRow; m <= maskRow; m++)
{
for (int n = -maskCol; n <= maskCol; n++)
{
sum += ptmp[(i + m) * mcols + j + n] * Template[(m + maskRow) * (2 * maskCol + 1) + n + maskCol];
}
}
sum /= 25;
if (sum > 255)
{
sum = 255;
}
m_pData[i * mcols + j] = sum;
}
}
// 释放内存
delete[] ptmp;
pDoc->UpdateAllViews(NULL);
}
```
7*7均值滤波:
```c++
void CMyView::OnJunzhi77()
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (pDoc->m_pDib == NULL)
{
AfxMessageBox("未打开图像");
return;
}
int mcols = pDoc->m_pDib->m_nCols;
int mrows = pDoc->m_pDib->m_nRows;
unsigned char* m_pData = pDoc->m_pDib->pImgData;
unsigned char* m_pDataOldPos = m_pData;
// 备份原始数据
BYTE* ptmp = new BYTE[mcols * mrows];
memcpy(ptmp, m_pData, mcols * mrows);
// 定义模板和掩码大小
int Template[49] = { 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1 };
int maskCol = 3;
int maskRow = 3;
// 对每个像素进行处理
for (int j = maskCol; j < mcols - maskCol; j++)
{
for (int i = maskRow; i < mrows - maskRow; i++)
{
int sum = 0;
for (int m = -maskRow; m <= maskRow; m++)
{
for (int n = -maskCol; n <= maskCol; n++)
{
sum += ptmp[(i + m) * mcols + j + n] * Template[(m + maskRow) * (2 * maskCol + 1) + n + maskCol];
}
}
sum /= 49;
if (sum > 255)
{
sum = 255;
}
m_pData[i * mcols + j] = sum;
}
}
// 释放内存
delete[] ptmp;
pDoc->UpdateAllViews(NULL);
}
```