C++ 动态数组索引和容量问题
C++ Dynamic Arrays Index and Capacity problems
我的家庭作业是让我建立一个动态数组和其他功能。我已经设法完成了其余的问题,但有一个错误我不明白...
目前如果我设置D[0]=11
和D[1]=12
,无论数组有多大,数组中的所有值都会变成12,它的容量也会变成12。
这些是我认为下面相关的代码,我会根据要求提供更多。
OUTPUT
template <class dynElem>
class dynarr {
private:
int capacity;
dynElem *A;
public:
dynarr() : capacity(0), A(NULL){};
dynarr(int N): capacity(N), A(new dynElem[N]){}
dynarr(const dynarr<dynElem> &other);
~dynarr();
dynarr<dynElem> & operator=( const dynarr<dynElem> &other);
dynElem & operator[](int ndx) throw(InvalidIndex);
int getCapacity();
void reserve(int newcap);
};
template <class dynElem>
dynarr<dynElem>::dynarr(const dynarr<dynElem> &other)
{
capacity = other.capacity;
A = new dynElem[capacity];
for (int i = 0; i < other.capacity; i++) {
A[i] = other.A[i];
}
}
template <class dynElem>
dynarr<dynElem>::~dynarr()
{
delete[] A;
}
test.cpp
int main()
{
dynarr<int> D(15);
std::cout << "The capacity of D is " << D.getCapacity() << std::endl;
D[0] = 11;
D[1] = 12;
std::cout << "D[0] = " << D[0] << std::endl; //12
std::cout << "D[1] = " << D[1] << std::endl; //12
std::cout << "The capacity of D is " << D.getCapacity() << std::endl; //12
return 0;
}
@someprogrammerdude 要求的额外代码:
template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
if (ndx < 0) {
throw InvalidIndex("Array Index is negative");
}
else if (ndx > capacity) {
throw InvalidIndex("Array Index is too large");
}
}
template <class dynElem>
int dynarr<dynElem>::getCapacity()
{
return capacity;
}
我很惊讶你的代码实际上编译时没有警告(我的编译器会冲我大喊大叫)。您 'forgot' 到 return 来自 operator[]
的值。这是你应该做的。
template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
if (ndx < 0) {
throw InvalidIndex("Array Index is negative");
}
else if (ndx > capacity) {
throw InvalidIndex("Array Index is too large");
}
return A[ndx]; // here we return the element.
}
顺便说一句,在现代 C++ 中,您的代码会略有不同(除了您永远不会 re-implement std::vector
的事实):
template<typename T>
class dynarr {
std::size_t _capacity =0; // set default values for initialisation
std::unique_ptr<T[]> ptr; // don't worry about de-allocation
public:
dynarr() = default; // no need to define a destructor either
dynarr(std::size_t N)
: _capacity(N), ptr(new T[N]) {}
dynarr(const dynarr&);
dynarr& operator=(const dynarr&);
T& operator[](size_t i); // no need for throw()
T const& operator[](size_t i) const; // version for const access
size_t capacity() const noexcept
{ return _capacity; }
void reserve(size_t new_capacity);
};
我的家庭作业是让我建立一个动态数组和其他功能。我已经设法完成了其余的问题,但有一个错误我不明白...
目前如果我设置D[0]=11
和D[1]=12
,无论数组有多大,数组中的所有值都会变成12,它的容量也会变成12。
这些是我认为下面相关的代码,我会根据要求提供更多。
OUTPUT
template <class dynElem>
class dynarr {
private:
int capacity;
dynElem *A;
public:
dynarr() : capacity(0), A(NULL){};
dynarr(int N): capacity(N), A(new dynElem[N]){}
dynarr(const dynarr<dynElem> &other);
~dynarr();
dynarr<dynElem> & operator=( const dynarr<dynElem> &other);
dynElem & operator[](int ndx) throw(InvalidIndex);
int getCapacity();
void reserve(int newcap);
};
template <class dynElem>
dynarr<dynElem>::dynarr(const dynarr<dynElem> &other)
{
capacity = other.capacity;
A = new dynElem[capacity];
for (int i = 0; i < other.capacity; i++) {
A[i] = other.A[i];
}
}
template <class dynElem>
dynarr<dynElem>::~dynarr()
{
delete[] A;
}
test.cpp
int main()
{
dynarr<int> D(15);
std::cout << "The capacity of D is " << D.getCapacity() << std::endl;
D[0] = 11;
D[1] = 12;
std::cout << "D[0] = " << D[0] << std::endl; //12
std::cout << "D[1] = " << D[1] << std::endl; //12
std::cout << "The capacity of D is " << D.getCapacity() << std::endl; //12
return 0;
}
@someprogrammerdude 要求的额外代码:
template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
if (ndx < 0) {
throw InvalidIndex("Array Index is negative");
}
else if (ndx > capacity) {
throw InvalidIndex("Array Index is too large");
}
}
template <class dynElem>
int dynarr<dynElem>::getCapacity()
{
return capacity;
}
我很惊讶你的代码实际上编译时没有警告(我的编译器会冲我大喊大叫)。您 'forgot' 到 return 来自 operator[]
的值。这是你应该做的。
template <class dynElem>
dynElem & dynarr<dynElem>::operator[](int ndx) throw(InvalidIndex)
{
if (ndx < 0) {
throw InvalidIndex("Array Index is negative");
}
else if (ndx > capacity) {
throw InvalidIndex("Array Index is too large");
}
return A[ndx]; // here we return the element.
}
顺便说一句,在现代 C++ 中,您的代码会略有不同(除了您永远不会 re-implement std::vector
的事实):
template<typename T>
class dynarr {
std::size_t _capacity =0; // set default values for initialisation
std::unique_ptr<T[]> ptr; // don't worry about de-allocation
public:
dynarr() = default; // no need to define a destructor either
dynarr(std::size_t N)
: _capacity(N), ptr(new T[N]) {}
dynarr(const dynarr&);
dynarr& operator=(const dynarr&);
T& operator[](size_t i); // no need for throw()
T const& operator[](size_t i) const; // version for const access
size_t capacity() const noexcept
{ return _capacity; }
void reserve(size_t new_capacity);
};