문제

나를 구현할 필요가 매트릭스와 운영하고 크기의 행렬은 변수가 있습니다.유일한 솔루션을 내놓았가 사용하는 링크 목록:

[pointer to this row, pointer to another row] -> [element 1,1; link to another element] -> [element 1,2,  link to another element] -> .... -> [nil]
     |
     v
[pointer to this row, pointer to another row] ...
     ...

하지만 그것이 나에게 조금 복잡한..더 나은(고 쉽)솔루션?

감사합니다!!

도움이 되었습니까?

해결책

한 가지 방법은 사용하는 것입니다 getmem 정확히 충분한 메모리를 할당합니다. GetMem은 널리 지원되는 것 같습니다.

const
    MAXMATRIXDATA: Word = 10000;
type
    TMatrixDataType = Word;
    TMatrixData = array[0..MAXMATRIXDATA] of TMatrixDataType;
    PMatrixData = ^TMatrixData;
    TMatrix = record
        Rows, Cols: Word;
        MatrixData: PMatrixData;
        end;
    PMatrix = ^TMatrix;

function CreateMatrix(Rows, Cols: Word): PMatrix;
var
    Ret: PMatrix;
begin
    New(Ret);
    Ret^.Rows := Rows;
    Ret^.Cols := Cols;
    GetMem(Ret^.MatrixData,Rows*Cols*SizeOf(TMatrixDataType));
    CreateMatrix := Ret;
end;

function GetMatrixData(Matrix: PMatrix; Row, Col: Word): TMatrixDataType;
begin
    GetMatrixData := Matrix^.MatrixData^[(Row*Matrix^.Cols)+Col];
end;

procedure SetMatrixData(Matrix: PMatrix; Row, Col: Word; Val: TMatrixDataType);
begin
    Matrix^.MatrixData^[(Row*Matrix^.Cols)+Col] := Val;
end;

다른 팁

모든 현대적인 파스칼은 변종(델파이)은 당신이 역(런타임즈)배열입니다.

는 경우에는 언어를 지원하지 않는 다차원의 동적 배열은 외에 헤어드라이어도 마련되어 있습을 해결 yourself:

var
  rows, cols, total, i, j : integer;
  cell : datatype;
begin
  rows := ...;
  cols := ...;
  total := rows * cols;
  matrix := ...(total);

  cell := matrix[i * cols + j]; // matrix[row=i,col=j]

end;

의이 종류는 해결 될 것보다 훨씬 더 빠르게 다음과 같은 링크 목록입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top