안선생의 개발 블로그
C++ 클래스를 이용한 배열 본문
가변배열에서 만든 C스타일로 만든 구조체로 만든 가변배열을 C++ 클래스를 이용한 배열로 바꿔보기
CArr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#pragma once
class CArr
{
private:
int* pint;
int count;
int maxcount;
public:
void PushBack(int Data);
void resize(int ResizeCount);
void Sort();
int& operator[] (int idx); // operator를 써줘야함
public:
CArr();
~CArr();
};
|
cs |
CArr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#include "CArr.h"
#include <assert.h>
#include <iostream>
using namespace std;
CArr::CArr() // 클래스 선언 범위 밖에서 지정했기때문에 CArr클래에 지정되어있는 생성자라는 뜻
//이니셜라이저로 초기화
: pint(nullptr)
, count(0)
, maxcount(2)
{
pint = new int[2]; // new 새로운 C++동적활당 malloc 이랑 같음
// int2개만큼 할당
}
CArr::~CArr()
{
//delete 동적할당 해제 free랑 같음
for (int i = 0; i < count; i++)
{
cout << pint[i] << endl;
}
delete[] pint; // 위에 인트 자료형을 여러게 선언했기 떄문에 []붙여줌 하나만 선언할시 []안붙여도 된다.
count = 0;
maxcount = 0;
}
void CArr::PushBack(int Data)
{
if (this->maxcount <= this->count)
{
resize(maxcount*2);
}
pint[this->count++] = Data;
}
void CArr::resize(int ResizeCount)
{
//현재 최대 수용량 보다 더 적은 수치로 확장하려는 경우
if (maxcount >= ResizeCount)
{
assert(nullptr);
}
// 1. 리사이즈 시킬 개수만큼 동적할당 한다.
int* pnew = new int[ResizeCount];
// 2 기존 공간에 있던 데이터들을 새로 할당한 공간으로 복사시킨다.
for (int i = 0; i < this->count; i++)
{
pnew[i] = this->pint[i];
}
// 3. 기존 공간은 메모리 해제
delete[] pint;
// 4. 배열이 새로 할당된 공간을 가리키게 한다.
this->pint = pnew;
//5 . maxcoyunt 변경점 적용
this->maxcount = ResizeCount;
}
void CArr::Sort()
{
while (1)
{
if (count < 1)
{
break;
}
bool finsih = true;
for (int i = 0; i < count-1; i++) {
if (pint[i] > pint[i+1])
{
int re = pint[i];
pint[i] = pint[i+1];
pint[i+1] = re;
finsih = false;
}
}
if (finsih == true)
{
break;
}
}
}
int& CArr::operator[](int idx)
{
return pint[idx];
}
|
cs |
CArr.h
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include<iostream>
#include "test.h"
#include<time.h>
#include "list.h"
#include "b.h"
#include "CArr.h"
using namespace std;
int main()
{
tArr arr = {};
initarr(&arr);
pushBack(&arr, 10);
pushBack(&arr, 20);
pushBack(&arr, 30);
srand(time(nullptr));
CArr c;
for (int i = 0; i < 15; i++)
{
int s1 = rand() % 100 + 1;
c.PushBack(s1);
}
c.Sort();
int a = 100;
c[1] = a;
return 0;
}
|
cs |
리스트 바꿔보기
CList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class Node
{
public:
Node* NextNode = nullptr;
int Data;
};
class CList
{
private:
Node* HeadNode;
int count;
public:
void PushBack(int Data);
void PushFornt(int Data);
public:
CList();
~CList();
};
|
cs |
CList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include "CList.h"
#include<iostream>
using namespace std;
CList::CList()
:HeadNode(nullptr)
, count(0)
{
}
CList::~CList()
{
Node* NewNode = HeadNode;
for (int i = 0; i < count; i++)
{
cout << NewNode->Data << "\n";
NewNode = NewNode->NextNode;
}
Node* pNode = HeadNode;
while (pNode->NextNode)
{
Node* fNode = pNode->NextNode;
delete[] pNode;
pNode = fNode;
}
}
void CList::PushBack(int Data)
{
Node* nNode = new Node[sizeof(Node)];
nNode->Data = Data;
nNode->NextNode = nullptr;
if (count == 0 )
{
HeadNode = nNode;
}
else
{
Node* pNode = this->HeadNode;
while (pNode->NextNode) {
pNode = pNode->NextNode;
}
pNode->NextNode = nNode;
}
count++;
}
void CList::PushFornt(int Data)
{
Node* nNode = new Node[sizeof(Node)];
nNode->Data = Data;
nNode->NextNode = HeadNode;
HeadNode = nNode;
count++;
}
|
cs |
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include<iostream>
#include "test.h"
#include<time.h>
#include "list.h"
#include "b.h"
#include "CArr.h"
#include "CList.h"
using namespace std;
int main()
{
CList s;
for (int i = 0; i < 10; i++)
{
s.PushFornt(i);
}
for (int i = 0; i < 10; i++)
{
s.PushBack(i);
}
return 0;
}
|
cs |
출처 : https://www.youtube.com/c/AssortRockGameAcademy
'C++' 카테고리의 다른 글
C++ 클래스 템플릿 (2) | 2022.08.27 |
---|---|
C++ 함수 템플릿 (0) | 2022.08.26 |
C++ 클래스 (0) | 2022.08.23 |
C++ 리스트 (0) | 2022.08.17 |
C++ 가변배열 (0) | 2022.07.20 |