【发布时间】:2015-06-05 11:07:03
【问题描述】:
首先我想为我的英语道歉。我的问题很奇怪,因为我正在尝试编写自己的 ArrayList 外观并像 Java 中的 List 一样工作,我知道这就像重新发明一个轮子,但我这样做是为了好玩并更好地理解它是如何工作的。所以像“使用 STL”、“使用向量”、“使用其他东西但不创建自己的列表”这样的每个答案都没有帮助。 所以从一开始,我就有自己的 ArrayList 模板,其中包含一些对我很重要的方法。我的 ArrayList 作为数组工作,我只使用一部分可用空间,如果我需要更多空间,我会创建新的更大的数组并复制所有旧元素(我相信它在 Java ArrayList 中的工作方式,如果我错了,请告诉我) .在数组中我只存储指针。它是保存和删除已知对象的好模板,如果我只需要读取存储的数据,但是当我尝试编写循环以读取、检查和删除列表中的指定元素时,就会出现真正的问题。我不能使用标准 for(int i=0;i
- Supporting "for each" on custom const native C++ container class
- supporting for each loop in classes
- C++ for each in on custom collections
- How to make the for each loop function in C++ work with a custom class
- Creating my own Iterators
- http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html
那么有人可以解释一下我应该在我的模板中写什么来支持每个功能。在许多示例中出现了 begin() 和 end() 函数,但没有人解释为什么这个名字,什么是返回类型,为什么,以及这个方法应该返回什么。这是我的模板代码,如果有问题,请告诉我。我将在我的其他应用程序中使用这段代码,因为对我来说,这个实现比向量更直观(我使用 Java 的时间肯定太长了:))
template <class T> class ArrayList {
public:
ArrayList() {
array = new T*[1000];
arraySize = 1000;
n = 0;
};
void add(T &arg) { //add new element at end
if (n == arraySize) {
increase();
}
array[n] = &arg;
n++;
};
void addAt(T &arg, unsigned int pos) { //add new element at specific position and override
if (pos >= 0 && pos <= n) {
if (pos == n) {
add(arg);
}
else {
array[pos] = &arg;
}
}
else {
throw "IndexOutOfBoundException";
}
};
void addAfter(T &arg, unsigned int pos) { //add new element between specific posittion and next element
pos++;
if (pos >= 0 && pos <= n) {
if (n == arraySize) {
increase();
}
for (unsigned int i = n; i > pos; i--) {
array[i] = array[i - 1];
}
array[pos] = &arg;
n++;
}
else {
throw "IndexOutOfBoundException";
}
};
void addList(ArrayList &list) { //add 'list' at the end
if (list.n > 0) {
while (list.n + n > arraySize) {
increase();
}
for (int i = 0; i < list.n; i++) {
array[n] = list.array[i];
n++;
}
}
};
void addListAfter(ArrayList &list, unsigned int pos) { //put 'list' inside list, start from 'pos'
pos++;
if (list.n > 0 && pos >= 0 && pos < n) {
while (list.n + n > arraySize) {
increase();
}
int m = n - 1;
while (m >= pos && m >= 0) {
array[m + list.n] = array[m];
m--;
}
for (int i = 0; i < list.n; i++) {
array[pos + i] = list.array[i];
}
n += list.n;
}
else {
throw "IndexOutOfBoundException";
}
};
void addListAfter(ArrayList &list, T &arg) { //put 'list' inside list, start after T, if T not exist 'list' will be added at the end
addListAfter(list, getIndex(arg));
};
void remove(T &arg, bool all) { //remove selected element if all=true remove all instance of object otherwise remove only first
if (all) {
int copies = 0;
for (int index = 0; index < n; index++) {
if (array[index] == &arg) {
copies++;
}
else if (copies != 0) {
array[index - copies] = array[index];
}
}
n -= copies;
if (copies == 0) {
throw "ArgumentNotFoundException";
}
while (arraySize - n >= 1000) {
decrease();
}
}
else {
remove(getIndex(arg));
}
};
void remove(unsigned int pos) { //remove element from specific position
if (pos >= 0 && pos < n) {
for (int i = pos; i < n - 1; i++) {
array[i] = array[i + 1];
}
n--;
if (arraySize - n >= 1000) {
decrease();
}
}
else {
throw "IndexOutOfBoundException";
}
};
void removeCopy(T &arg) { //leaves only one instance of an object and remove all other
int copies = -1;
for (int index = 0; index < n; index++) {
if (array[index] == &arg) {
copies++;
}
else if (copies > 0) {
array[index - copies] = array[index];
}
}
n -= copies;
if (copies == -1) {
n--;
throw "ArgumentNotFoundException";
}
while (arraySize - n >= 1000) {
decrease();
}
};
void repair() { //leaves only single instance of each object
for (int i = 0; i < n; i++) {
removeCopy(*array[i]);
}
};
void clear() { //remove all object from list
for (int i = 0; i < n; i++) {
array[i] = NULL;
}
n = 0;
};
T* get(unsigned int pos) { //return object on selected position
if (pos >= 0 && pos < n) {
return array[pos];
}
else {
throw "IndexOutOfBoundException";
}
};
unsigned int getIndex(T &arg) { //return position of selected object
unsigned int index = 0;
while (&arg != array[index] && index < n) {
index++;
}
if (index == n) {
throw "ArgumentNotFoundException";
}
return index;
};
ArrayList getSubList(unsigned int first, unsigned int last, bool deepCopy) { //return new list contains 'deep copy'/'copy reference' of all elements from (include) first to (include) last. If deepCopy=true function return deep copy, otherwise return copy of reference.
if (first < last&&first >= 0 && last < n) {
ArrayList<T> ret;
for (unsigned int i = first; i <= last; i++) {
if (deepCopy) {
ret.add(*new T(*array[i]));
}
else {
ret.add(*array[i]);
}
}
return ret;
}
throw "IndexOutOfBoundException";
};
unsigned int size() { //return size of list
return n;
};
bool isEmpty() {
return n == 0;
};
T *begin() {
return &*array[0];
}
T *end() {
return &*array[n];
}
private:
unsigned int arraySize; //actual size of array
unsigned int n; //number of elements in array
T** array;
void increase() { //increase size of array about 1000
if (arraySize + 1000 <= LONG_MAX) {
T** newArray = new T*[arraySize + 1000];
for (unsigned int i = 0; i < arraySize; i++) {
newArray[i] = array[i];
}
delete[] array;
array = newArray;
arraySize += 1000;
}
else {
throw "ArraySizeOutOfBoundException";
}
};
void decrease() { //decrease size of array about 1000
if (arraySize - 1000 > 0) {
arraySize -= 1000;
T** newArray = new T*[arraySize];
for (unsigned int i = 0; i < arraySize; i++) {
newArray[i] = array[i];
}
delete[] array;
array = newArray;
}
else {
throw "ArraySizeOutOfBoundException";
}
};
};
【问题讨论】:
-
提示:
iterator是一种不同的类型,但与您的容器密切相关。 “我正在尝试编写自己的 ArrayList 外观和工作方式,就像 Java 中的 List ...” 你到底为什么要这样做?这肯定会导致 c++ 代码一团糟,而且根本没有必要。 C++ 已经提供了各种容器模板类,它们做得很好,你想要什么。 -
@πάνταῥεῖ 提问者说他们这样做只是为了好玩。这个主题在 SO 上似乎很敏感,但我看不出它与他们的问题有什么关系(从设计的角度来看,你当然是对的)。
-
@scry 自己实现这些通常不好玩。这就是为什么我们把它留给 c++ 标准库实现的原因之一。
-
这是一种简单但不一定有效的方法。首先,您创建某种
operator[]来访问任意索引。然后,创建一个包含指向实例的指针并保存索引的迭代器。它可以使用operator[]抓取当前元素并增加索引。
标签: c++ loops arraylist foreach custom-lists