对类指针数组进行 qsorting
我读过很多关于如何做到这一点的材料。不幸的是,他们都求助于使用 qsort(通常是排序)以外的东西。然而,这对我来说是不可能的,因为我不允许使用算法库中的任何内容。我必须从头开始实施这个,几乎。我得到了什么:
class String {
public:
char* string;
int size;
String(char* string=0, int size=0) {
this->string = string;
this->size = size;
}
~String() {
delete [] string;
}
};
int compare(void* obj1, void* obj2) {
String* str1 = ((String*) obj1)->getColumn(column);
String* str2 = ((String*) obj2)->getColumn(column);
int i = strcmp(str1->string, str2->string);
delete str1;
delete str2;
return i;
}
class ArrayList {
int count;
int arraySize;
public:
String** list;
ArrayList() {
count = 0;
arraySize = 10;
list = new String*[arraySize];
}
~ArrayList() {
while(size()) {
delete remove();
}
}
void add(String* s) {
if(count==arraySize)
grow();
list[count++] = s;
}
String* remove() {
return list[count--];
}
String* get(int i) {
return list[i];
}
int size() {
return count;
}
private:
void grow() {
String** temp = new String*[arraySize*=2];
for(int i=0; i<count; i++) {
temp[i] = list[i];
}
delete [] list;
list = temp;
}
};
以及我对 qsort 的(当前)调用,尽管我尝试了很多组合:
qsort(list->list, list->size, sizeof(String**), compare);
无论我传递什么,我总是得到的错误:
argument of type ‘int (ArrayList::)()’ does not match ‘size_t’
我在此处复制的代码并不包含所有内容,但我已验证所有部分均按预期工作,因此不必担心缺少方法等。
I've read a lot of material on how to do this. Unfortunately, they all resort to using something other than qsort (sort, usually). However, this is impossible for me as I'm not allowed to use anything from the algorithm library. I have to implement this from scratch, pretty much. What I've got:
class String {
public:
char* string;
int size;
String(char* string=0, int size=0) {
this->string = string;
this->size = size;
}
~String() {
delete [] string;
}
};
int compare(void* obj1, void* obj2) {
String* str1 = ((String*) obj1)->getColumn(column);
String* str2 = ((String*) obj2)->getColumn(column);
int i = strcmp(str1->string, str2->string);
delete str1;
delete str2;
return i;
}
class ArrayList {
int count;
int arraySize;
public:
String** list;
ArrayList() {
count = 0;
arraySize = 10;
list = new String*[arraySize];
}
~ArrayList() {
while(size()) {
delete remove();
}
}
void add(String* s) {
if(count==arraySize)
grow();
list[count++] = s;
}
String* remove() {
return list[count--];
}
String* get(int i) {
return list[i];
}
int size() {
return count;
}
private:
void grow() {
String** temp = new String*[arraySize*=2];
for(int i=0; i<count; i++) {
temp[i] = list[i];
}
delete [] list;
list = temp;
}
};
And my (current) call to qsort, though I've tried many combinations:
qsort(list->list, list->size, sizeof(String**), compare);
The error I ALWAYS get, not matter what I pass:
argument of type ‘int (ArrayList::)()’ does not match ‘size_t’
The code I've copied here doesn't include everything, but I've verified that all of the pieces work as intended, so don't worry about missing methods and such.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用了
list->size
而不是list->size()
。代码应该是:You have used
list->size
instead oflist->size()
. The code should be: