我试图将二维数组复制到另一个大小的另一个数组。 例如: 第一个包含4行和4列的数组:
1 2 3 4
5 6 7 8
9 0 1 2
3 4 5 6
第二个数组,包含2行和8列:
1 2 3 4 5 6 7 8
9 0 1 2 3 4 5 6
如果新数组中的元素多于第一个元素,那么该函数将用0填充它
这是我所做的功能,但索引的问题。 如何以正确的方式写出来?
void changearr(int **ppm, int size1, int size2, int size_1, int size_2)
{
int **temp = new int*[size_1];
for (int i = 0; i < size_1; i++)
temp[i] = new int[size_2];
int z = 0;
for (int i = 0; i < size_1; i++, z++)
{
for (int j = 0, k = 0; j < size_2; j++, k++)
{
if (i < size_1 || j < size_2)
{
temp[i][j] = ppm[z][k];
}
else
temp[i][j] = 0
}
}
答案 0 :(得分:8)
哦,这是一个很棒的编程难题。我的解决方案是将两个数组展平并复制它们。
template <typename T>
static constexpr T* begin(T& value) noexcept
{
return &value;
}
template <typename T, ::std::size_t N>
static constexpr typename ::std::remove_all_extents<T>::type*
begin(T (&array)[N]) noexcept
{
return begin(*array);
}
template <typename T>
static constexpr T* end(T& value) noexcept
{
return &value + 1;
}
template <typename T, ::std::size_t N>
static constexpr typename ::std::remove_all_extents<T>::type*
end(T (&array)[N]) noexcept
{
return end(array[N - 1]);
}
int a[4][4];
int b[2][8];
::std::copy(begin(a), end(a), begin(b));
答案 1 :(得分:7)
为什么不从输入矩阵创建临时线性数组,然后使用它来填充输出矩阵:
void changearr ( int** ppm, int old_row, int old_col, int new_row, int new_col )
{
int* temp_linear = new int[old_row * old_col];
int k = 0;
for ( int i = 0; i < old_row; i++ )
{
for ( int j = 0; j < old_col; j++ )
{
temp_linear[k++] = ppm[i][j];
}
}
int** temp = new int* [new_row];
for ( int i = 0; i < new_row; i++ )
{
temp[i] = new int[new_col];
}
k = 0;
for ( int i = 0; i < new_row; i++ )
{
for ( int j = 0; j < new_col; j++ )
{
if ( k < old_row * old_col )
{
temp[i][j] = temp_linear[k++];
}
else
{
temp[i][j] = 0;
}
}
}
}
答案 2 :(得分:6)
您可以使用简单的临时容器:
#include <iostream>
#include <deque>
#include <array>
int main()
{
std::array<std::array<int,4>,4> first {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; //4x4
std::array<std::array<int,8>,2> second; //2x8
std::deque<int> temp; //temporary container
for(auto x : first)
for(auto y : x)
temp.push_back(y); //push everything from first to deque
for(auto& x : second)
for(auto& y : x)
{
y = temp.front(); //move from deque to second and pop()
temp.pop_front();
}
}
答案 3 :(得分:4)
2D数组不与指针指针相同!作为2D数组,只需一个接一个地存储行,您只需要复制元素:
#include <iostream>
void trans(int* orig, int rows1, int cols1, int *resul, int rows2, int cols2) {
int tot1 = rows1 * cols1;
int tot2 = rows2 * cols2;
int tot = tot1;
if (tot2 < tot) tot = tot2;
// copy the smallest size from both arrays
for(int i=0; i<tot; i++) {
resul[i] = orig[i];
}
// eventually add 0 to fill resul array
for(int i=tot; i<tot2; i++) {
resul[i] = 0;
}
}
int main()
{
int orig[4][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6};
// control initial array
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++) {
std::cout << orig[i][j];
}
std::cout << std::endl;
}
int resul[2][8];
// pass arrays as 1D array pointers!
trans(reinterpret_cast<int *>(&orig), 4, 4, reinterpret_cast<int *>(&resul), 2, 8);
// control converted array
for (int i=0; i<2; i++) {
for (int j=0; j<8; j++) {
std::cout << resul[i][j];
}
std::cout << std::endl;
}
return 0;
}
它按预期给出:
1234
5678
9012
3456
12345678
90123456
答案 4 :(得分:3)
如您所知,C / C ++ n维数组在内存中具有线性表示。因此,如果您打开此类表示,您将获得以下好处:
std::vector<int> v1{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
for(size_t r = 0; r < 4; r++)
for(size_t c = 0; c < 4; c++)
std::cout << v1[r*4+c];
std::vector<int> v2(v1);
for(size_t r = 0; r < 2; r++)
for(size_t c = 0; c < 8; c++)
std::cout << v2[r*8+c];
答案 5 :(得分:3)
这是一个非常简单的方法:
void
changearr(
int **in,int in_size1,int in_size2,
int **out,int out_size1,int out_size2
)
{
int in_n = in_size1*in_size2;
int out_n = out_size1*out_size2;
int n = min(in_n,out_n);
int i = 0;
for (; i!=n; ++i) {
out[i/out_size2][i%out_size2] = in[i/in_size2][i%in_size2];
}
for (; i!=out_n; ++i) {
out[i/out_size2][i%out_size2] = 0;
}
}
这个想法只是线性迭代索引并根据需要计算输入和输出数组的相应索引。