• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

10.6 컨테이너 클래스 숙제 피드백

23.02.04 20:23 작성 조회수 147

0

안녕하세요 강의를 듣고있는 학생입니다.

10.6에서 컨테이너 클래스의 개념을 설명해주시고

IntArray를 직접 구현해보는 숙제를 내주셔서 직접 머리를 짜내서 코드를 구현해봤습니다.

이 글을 보신 숙련자 분들께 제가 구현한 intArray를 피드백 받고싶어서 글을 작성합니다.

  1. #include <iostream>

  2. #include <vector>

  3. using namespace std;

  4. class IntArray

  5. {

  6. private:

  7. int m_length = 0;

  8. int* m_data = nullptr;

  9. public:

  10. IntArray(const int length)

  11. :m_length(length)

  12. {

  13. m_data = new int[m_length];

  14. }

  15. IntArray(const std::initializer_list<int>& list)

  16. :IntArray(list.size())

  17. {

  18. int count = 0;

  19. for (auto& ele : list)

  20. {

  21. //cout << "list" << endl;

  22. m_data[count] = ele;

  23. count++;

  24. }

  25. }

  26. ~IntArray()

  27. {

  28. delete[] m_data;

  29. }

  30. void initialize(const std::initializer_list<int>& list)

  31. {

  32. delete[] m_data;

  33. m_length = list.size();

  34. m_data = new int[m_length];

  35. if (m_data != nullptr)

  36. {

  37. int count = 0;

  38. for (auto& ele : list)

  39. {

  40. m_data[count] = ele;

  41. count++;

  42. }

  43. }

  44. }

  45. void reset()

  46. {

  47. delete[] m_data;

  48. m_data = nullptr;

  49. }

  50. void resize(const int size)

  51. {

  52. m_length = size;

  53. }

  54. void insertBefore(const int & value, const int & ix)

  55. {

  56. m_length += 1;

  57. int *n_data = new int[m_length];

  58. int i = ix;

  59. for (int n = 0; n < ix; n++)

  60. n_data[n] = m_data[n];

  61. n_data[ix] = value;

  62. for (i; i < m_length; i++)

  63. {

  64. n_data[i + 1] = m_data[i];

  65. }

  66. delete[] m_data;

  67. m_data = n_data;

  68. }

  69. void remove(const int& ix)

  70. {

  71. m_length -= 1;

  72. int* n_data = new int[m_length];

  73. int count = ix;

  74. for (int i = 0; i < ix; i++)

  75. {

  76. n_data[i] = m_data[i];

  77. }

  78. for (count; count < m_length; count ++)

  79. {

  80. n_data[count] = m_data[count+1];

  81. }

  82. delete[] m_data;

  83. m_data = n_data;

  84. }

  85. void push_back(const int& value)

  86. {

  87. this->m_length += 1;

  88. int* n_data = new int[m_length];

  89. for (int i = 0; i < m_length; i++)

  90. n_data[i] = m_data[i];

  91. n_data[m_length-1] = value;

  92. delete[] m_data;

  93. m_data = n_data;

  94. }

  95. friend ostream& operator << (ostream& out, IntArray& ia)

  96. {

  97. for (int i = 0; i < ia.m_length; i++)

  98. out << ia.m_data[i] << " ";

  99. out << endl;

  100. return out;

  101. }

  102. };

  103. int main()

  104. {

  105. IntArray my_arr{ 1, 3, 5, 7, 9 };

  106. cout << my_arr << endl;

  107. my_arr.insertBefore(10, 1);

  108. cout << my_arr << endl;

  109. my_arr.remove(2);

  110. cout << my_arr << endl;

  111. my_arr.push_back(13);

  112. cout << my_arr << endl;

  113. my_arr.initialize({ 1, 2, 3 });

  114. cout << my_arr << endl;

  115. return 0;

}

피드백 받고싶은 이유는 함수를 구현할 때 Array에 int를 추가, 삭제, 수정을 할때

새로 동적할당을 받은 뒤에 그 공간에 기존 데이터를 복사해주며 추가,삭제,수정 해줄 부분을 해주고

기존 동적할당 공간은 지우고 기존 객체가 가르키는 포인터를 새로 할당한 공간으로 설정 해주었습니다.

선생님 께서 예시로 작성하는 코드 짜임새와 코드라인수에 비해

제 머리속에서 나온 그대로를 코드로 담은거라 너무 조잡 난해하다는 생각이 들었습니다.

그래서 코드를 더 간단하게 짜려면 이 내용을 어떤식으로 수정하면 좋을지 피드백 받고싶습니다.

혹은 코드에 아쉬운점들을 고수분들의 관점으로 보고 말씀해주시면 배워보고싶습니다 감사합니다.

답변 1

답변을 작성해보세요.

0

강민철님의 프로필

강민철

2023.02.07

잘 작성하신 듯 합니다 :) 멋집니다.