动态2D阵列,偏移计算 [英] Dynamic 2D Array, Offset Calculation

查看:102
本文介绍了动态2D阵列,偏移计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends ..

只是为了掌握存储顺序的概念我正在尝试为MultiDimentional数组编写代码。



以下是代码..

  #include   < span class =code-keyword><   iostream  >  
#include < iomanip >
使用 namespace :: std;

// 为多维数组分配内存

void getData( int ** arr, int 行, int col){
int offset;
for int i = 0 ; i< row; i ++)
{
for int j = 0 ; j< col; j ++){
offset = i * col + j;
cin>> *(*(arr + offset));
}
}
}
void printData( int ** arr, int 行, int col){
for int i = 0 ; i< row; i ++)
{
for int j = 0 ; j< col; j ++){
int offset = i * col + j;
cout<< setw( 5 )<< *(*(arr + offset));
cout<< \ n;
}
}
}

int main(){
int ** board;
int rows = 2 ,cols = 2 ;
// 创建董事会行
board = new int * [rows];
// 创建电路板的cols
for int i = 0 ; i< rows; i ++)
{
board [i] = new int [cols];
}
getData(board,rows,cols);
cout<< \ n;
printData(board,rows,cols);

return 0 ;
}





我知道偏移的通用公式是=开始行数*总列数+开始列数。在这里,我想要你的建议..我想使用偏移量进入3或4维...

此外,在不同的书籍中,偏移的计算是不同的..所以很困惑..



在我的代码中,当我输入第3个元素程序崩溃..

我希望你能理解这个问题..

敏锐地等待你的回复..

谢谢 -



更新.. !!

  #include   <   iostream  >  
使用 namespace :: std;

void getData( int * arr, int 行, int col){ // by row
int offset = - 1 ;
for int i = 0 ; i< row; i ++)
{
for int j = 0 ; j< col; j ++)
{
offset = i * col + j; // 行专业:起始行* total_col +起始col
cin>> *(arr + offset);
}
}
cout<< ENDL;
}
void printData( int * ar, int 行, int col){ // 按col
for int i = 0 ; i< col; i ++)
{
for int j = 0 ; j< row; j ++)
{
int offset = j * col + i; // col major:start_row * total_cols + start_col
cout<< *(ar + offset)<< ENDL;
}
cout<< \ n \ n;
}
cout<< ENDL;
}

int main(){
int rows = 2 ;
int cols = 2 ;
int * weed = new int [行×COLS];
getData(weed,rows,cols);
printData(weed,rows,cols);

return 0 ;
}





如果我使用单指针它就像一个魅力......:/

但不是2指针的情况..

请帮助..

解决方案

名称 offset 有点误导,因为它通常是指字节偏移量。在您的情况下,您只是在一维数组中计算元素索引。你正在做的(正确)是将二维数组的元素映射到一维数组。而且你是通过将每行的元素存储在一个连续的单元格区域中来实现的。例如,对于具有两行和四列的数组,您将按以下顺序存储元素:

 R0C0 R0C1 R0C2 R0C3 R1C0 R1C1 R1C2 R1C3 



RxCy表示行x和列y的元素。这就是你如何得出这个公式



(列数)* rowIndex + colIndex



您可以轻松地将该方案扩展到更多维度。例如,在三维数组中,我们可以调用我们的维度:列,行,平面。公式为:



(行数*列数)* planeIndex +(列数)* rowIndex + columnIndex



根据存储的连续维度,公式略有不同。例如,您可以决定连续存储 plane 维,然后是列,最外面的行。对于此存储顺序,公式为:



(平面数*列数)* rowIndex +(平面数)* columnIndex + planeIndex



两种存储方案都不同。但只要您使用相同的方案进行存储和检索,一切都会正常工作。但是,存储维度的顺序会影响处理器缓存的使用方式,并可能对大型数值计算的处理时间产生影响。






这绝对不正确。如果多维数组是静态的,那么您可以计算并使用数组库中anz元素的偏移量。 但是,如果数组是按逻辑分配的,则根本无法计算任何偏移量!那是因为,整个数组存储在一块连续的记忆中。 (只需在谷歌图片中键入c ++动态数组),看看它是如何放入内存的。



希望这有帮助,

JK

Hello Friends ..
Just to grasp the concept of storage ordering i'm trying to write a code for MultiDimentional array.

Here is the code ..

#include <iostream>
#include <iomanip>
using namespace::std;

// Allocating Memory For Multi-Dimentional Array

void getData(int **arr, int row, int col){
    int offset;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++){
			offset = i * col + j;
			cin >> *(*(arr + offset));
		}
	}
}
void printData(int **arr, int row, int col){
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++){
			int offset = i * col + j;
			cout << setw(5) << *(*(arr + offset));
			cout << "\n";
		}
	}
}

int main(){
	int **board;
	int rows = 2, cols = 2;
	// creating the rows of board
	board = new int* [rows];
	// creating the cols of the board
	for (int i = 0; i < rows; i++)
	{
		board[i] = new int[cols];
	}
	getData(board, rows, cols);
	cout << "\n";
	printData(board, rows, cols);

	return 0;
}



I know the General Formula for offset is = Starting No Of Row * Total Columns + Starting No Of Column. Here i want your suggestions.. I want to go in 3 or 4 dimensions using offset ..
Moreover in different books, calculation for offset is differ .. So puzzled ..

in my code when i enter 3rd element the program Crashes ..
I hope you can understand the problem..
Keenly waiting your reply ..
Thanks -

UPDATED .. !!

#include <iostream>
using namespace::std;

void getData(int *arr, int row, int col){ // by row
	int offset = -1;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			offset = i*col + j; // for row major: starting row * total_col + starting col
			cin >> *(arr + offset);
		}
	}
	cout << endl;
}
void printData(int *ar, int row, int col){ // by col 
	for (int i = 0; i < col; i++)
	{
		for (int j = 0; j < row; j++)
		{
			int offset = j *col + i; // col major: start_row * total_cols + start_col
			cout << *(ar + offset) << endl;
		}
		cout << "\n\n";
	}
	cout << endl;
}

int main(){
	int rows = 2;
	int cols = 2;
	int *weed = new int[rows*cols];
	getData(weed, rows, cols);
	printData(weed, rows, cols);
	
	return 0;
}



If i use single pointer it works like a charm .. :/
But not in the case of 2 pointers ..
Please Help ..

解决方案

The name offset in your code is a little misleading as it normally refers to a byte offset. In your case you are simply calculating the element index in a one-dimensional array. What you are doing (correctly) is to map the elements of a two-dimensional array to a one-dimensional array. And you are doing it by storing the elements of each row in a contiguous range of cells. For example, for an array with two rows and four columns you would store the elements in the following order:

R0C0 R0C1 R0C2 R0C3 R1C0 R1C1 R1C2 R1C3


RxCy denoting the element of row x and column y. And that is how you come to the formula of

(Number of columns) * rowIndex + colIndex

You can easily expand that scheme to more dimensions. For example, in a three-dimensional array we might call our dimensions: column, row, plane. And the formula would be:

(Number of rows * number of columns) * planeIndex + (number of columns) * rowIndex + columnIndex

Depending on which dimension is stored contiguous, the formula is a little different. For example, your could decide to store the plane dimension contiguously, then the columns, and outermost the rows. For this storage order the formula would be:

(Number of planes * number of columns) * rowIndex + (number of planes) * columnIndex + planeIndex

Both storage schemes are different. But as long as you use the same scheme for storing and retrieval, everything will work fine. The order in which dimensions are stored has however an influence on how the processor cache will be used and may have an impact on processing time for large numerical computations.


Hi,

this definitely is not correct. If the multidimensional array is static, then you can compute and use offset of anz element from the array base. But, if the array is dznamically allocated, you cannot compute any offset at all! That is because, the whole array is not stored in one continual piece of memeory. (Just type "c++ dynamic array" to google pictures) to see how it is placed in the memory.

Hope this helps,
JK


这篇关于动态2D阵列,偏移计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆