U E D R , A S I H C RSS

Random Walk2/Vector로2차원동적배열만들기

DeleteMe 페이지 이름으로 MultidimensionalArray가 더 좋지 않을까요?

페이지 수정 필요.

STL/vector/CookBook 의 내용과 통합해야 합니다.

음..; 재동이한테 2차원 동적 배열 만드는 방법을 제공한 이유로..; 책임을 지겠습니다 ㅠ.ㅠ 근데 다 그렇게 쓰던데..--;

vector 좀 들여다 보다가 대충 만들어봤습니다. 고칠거 있으면 마음-_-껏 고쳐 주세요. 행이랑 열 입력 받아서 모두 0으로 초기화하는겁니다

~cpp 
#include <iostream>
#include <vector>
using namespace std;

vector< vector<int> > ar;  // 반드시 공백 줘야 한다! 안주면 에러난다.

void Alloc(int nRow, int nCol);
void SetArrayAsZero(int nRow, int nCol);
void Show(int nRow, int nCol);

int main()
{
	int row, col;
	cout << "행 " ;
	cin >> row;
	cout << "\n열 : ";
	cin >> col;
	Alloc(row, col);
	return 0;
}

void Alloc(int nRow, int nCol)
{
	ar.resize(nRow);

	for(int i = 0 ; i < nRow ; i++)
		ar[i].resize(nCol);
}

위의 방법 외에도 클래스로 감싸주고, 내부적으로는 1차원 배열을 쓰는 방법이 있겠죠. row*width+col로 특정 원소를 레퍼런스할 수 있습니다.

see also:

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:50
Processing time 0.0302 sec