배열과 현재 크기를 사용하여 객체 컬렉션을 저장하는 템플릿 클래스가 있습니다. 나는 삽입물을 알아 내고 제거했다. isEmpty
기능에 문제가 있습니다. 그것은 마치 3과 5를 추가하고 3과 5를 제거한다고해도 false
을 반환합니다 (비어 있지 않은 것처럼). 그러면 true
이라고합니다.배열에 삽입하는 방법? C++
#include <cstdio>
#include <iostream>
#include <cstdlib>
/************************************************************/
// Local includes
/************************************************************/
// Using declarations
using namespace std;
/************************************************************/
template <class T>
class Collection
{
public:
Collection (int mArraySize)
{
m_size = mArraySize;
array = new T[mArraySize];
reset();
}
/************************************************************/
int
size()
{
return nextEmpty;
}
/************************************************************/
void
reset()
{
nextEmpty = 0;
nextToRead = 0;
}
/************************************************************/
void
insert(const T& a)
{
if (nextEmpty < m_size)
{
array[nextEmpty++] = a;
}
}
/************************************************************/
bool
isEmpty()
{
if(m_size == 0)
{
return true;
}
else
{
return false;
}
}
/************************************************************/
void
makeEmpty()
{
m_size = 0;
array = NULL;
}
/************************************************************/
void
remove(const T& r)
{
int i = 0;
for (i = 0; i < m_size; i++)
{
if (array[i] == r)
{
break;
}
}
while (i++ < m_size)
{
array[i - 1] = array[i];
}
m_size--;
}
/************************************************************/
void
contains(T array, T target)
{
for(int i = 0; i < m_size; i++)
{
}
}
/************************************************************/
T&
get()
{
return array[nextToRead++];
}
/************************************************************/
private:
T* array;
int nextEmpty;
int nextToRead;
int m_size;
};
/************************************************************/
#include <iostream>
#include <iterator>
/****************************************************************************/
// Local includes
#include "header.h"
/****************************************************************************/
// Using declarations
using namespace std;
/****************************************************************************/
// Prototypes, typedefs, etc.
bool
isEmpty();
void
makeEmpty();
void
insert();
void
remove();
void
contains();
void
testerFcn();
/****************************************************************************/
int main(int argc, char* pArgs[])
{
testerFcn();
return EXIT_SUCCESS;
}
/************************************************************/
void
testerFcn()
{
Collection <int> testArray(15);
// insert test
cout << "Enter numbers to add to array (enter negative number to finish): " << endl;
for(;;)
{
int n;
cin >> n;
if (n < 0) {
break;
}
testArray.insert(n);
}
// remove test
cout << "Enter value to remove, (enter negative number to finish): " << endl;
for(;;)
{
int n;
cin >> n;
if (n < 0) {
break;
}
testArray.remove(n);
}
cout << "Is the array empty: 0 = false, 1 = true ==> " << testArray.isEmpty() << endl;
// print what we have
cout << "New Array: " << endl;
for (int i = 0; i < testArray.size(); i++)
{
cout << i << ":" << testArray.get() << endl;
}
}
이 숙제가 있습니까? 벡터를 사용하지 않는 이유는 무엇입니까? – Bart
실제 코드입니까? 컴파일 오류로 가득 차 있습니다. 컴파일을 시도하고 테스트를 실행 해 보셨습니까? 나는 이것이 숙제라고 생각하고 있습니다. 그래서 저는 지금 대답하지 않습니다. 이것이 숙제 인 경우 내가 드릴 수있는 가장 중요한 조언은 디버거 사용법을 배우고 조기 및 자주 컴파일하며 기능에 대한 단위 테스트를 작성하는 것입니다. – Chad
예. 우리는 다른 사람들과 일할 수 있습니다. 그리고 예, 이번 코스에서는 C++을 사용하기 시작 했으므로 아직 벡터를 배웠습니다. 나는 그들이 무엇인지는 알지만 기술적으로 그것을 배웠다고 생각하지 않는다. 나는 그것을 사용하지 말아야한다. – user1202950