我正在制作一个用模板练习的程序,遇到了一个奇怪的问题:
[code]
#include<iostream>
#include<new>
using namespace std;
template<class T>
class Vector {
private:
int Vector_Size;
T* Vector_Element;
int Free_Vector_Elements;
int Occupied_Vector_Elements;
int* Free_Element_List;
int* Occupied_Element_List;
public:
Vector() : Vector_Size(0), Vector_Element(0) {}
Vector(T Size) : Vector_Size(Size), Vector_Element(new T[Size]) {
for (int Element = 0; Element < Vector_Size; ++Element) { Vector_Element[Element] = 0; }
}
~Vector() {
delete[] Vector_Element;
delete[] Free_Element_List;
delete[] Occupied_Element_List;
}
T& operator[](int Element);
const T& operator[](int Element) const;
void Resize_Vector(int Rellocated_Size);
void Scan_Vector();
void Show_Vector_Info();
};
template<class T>
T& Vector<T>::operator[](int Element) { return(Vector_Element[Element]); }
template<class T>
const T& Vector<T>::operator[](int Element) const { return(Vector_Element[Element]); }
template<class T>
void Vector<T>::Resize_Vector(int Rellocated_Size) {
if (Rellocated_Size < Vector_Size) { cout << "Error:smaller size rellocated\n"; return; }
int Element;
T* Rellocated_Vector = new (nothrow) T[Rellocated_Size];
if (Rellocated_Vector == 0) { cout << "Error:memory could not be allocated\n"; return; }
for (Element = 0; Element < Vector_Size; ++Element) {
Rellocated_Vector[Element] = Vector_Element[Element];
}
for (int Clear = 0; (Clear + Element) < Rellocated_Size; ++Clear) { Rellocated_Vector[Clear + Element] = 0; }
delete[] Vector_Element;
Vector_Element = Rellocated_Vector;
Vector_Size = Rellocated_Size;
}
template<class T>
void Vector<T>::Scan_Vector() {
Free_Vector_Elements = 0;
Occupied_Vector_Elements = 0;
int Element, Free_Counter = 0, Occupied_Counter = 0;
for (Element = 0; Element < Vector_Size; ++Element) {
if (Vector_Element[Element] == 0) { ++Free_Vector_Elements; }
else if (Vector_Element[Element] != 0) { ++Occupied_Vector_Elements; }
}
Free_Element_List = new int[Free_Vector_Elements];
Occupied_Element_List = new int[Occupied_Vector_Elements];
for (Element = 0; Element < Vector_Size; ++Element) {
if (Vector_Element[Element] == 0) { Free_Element_List[Free_Counter] = Element; ++Free_Counter; }
else if (Vector_Element[Element] != 0) { Occupied_Element_List[Occupied_Counter] = Element; ++Occupied_Counter; }
}
}
template<class T>
void Vector<T>::Show_Vector_Info() {
if (Free_Vector_Elements != 0) {
cout << "There are: " << Free_Vector_Elements << " free elements" << endl;
for (int Element = 0; Element < Free_Vector_Elements; ++Element) {
cout << "Element " << Free_Element_List[Element] << ": Free" << endl;
}
}
if (Occupied_Vector_Elements != 0) {
cout << endl;
cout << "There are: " << Occupied_Vector_Elements << " occupied elements" << endl;
for (int Element = 0; Element < Occupied_Vector_Elements; ++Element) {
cout << "Element " << Occupied_Element_List[Element] << ": " << Vector_Element[Occupied_Element_List[Element]] << endl;
}
}
}
int main() {
Vector<int> Test_Vector(5);
Test_Vector.Resize_Vector(10);
Test_Vector.Scan_Vector();
Test_Vector.Show_Vector_Info();
return 0;
}
[/code]
第49行中的for循环行为很奇怪,即:当我将Clear设置为0时程序正常执行但是在制作程序流程表之后我注意到它应该清除第4个元素以及哪个是不需要的行为但是它没有奇怪的是,它的工作就好像它清除了第五个元素,但是当我将Clear设置为1时,元素5不会被清除,这让我困惑为什么?第一次循环迭代 将Rellocated_Vector设置在计数器+元素0 + 4处,理论上应该清除第4个元素,但是它清除第5个元素是什么原因?
除此之外,我的问题是: - 我的代码可以理解吗? - 我的代码干净了吗? - 我的程序有效还是我可以删除某些部分以使其更好? - 我可以在哪里找到中等到高难度的练习模板,因为我发现这些模板太容易了,或者我自己就像上面那样制作它们需要几个小时来提出具有挑战性的东西。
答案 0 :(得分:-2)