对类指针数组进行 qsorting

发布于 2024-12-05 15:11:58 字数 1975 浏览 2 评论 0原文

我读过很多关于如何做到这一点的材料。不幸的是,他们都求助于使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2024-12-12 15:11:58

您使用了 list->size 而不是 list->size()。代码应该是:

qsort(list->list, list->size(), sizeof(String**), compare);

You have used list->size instead of list->size(). The code should be:

qsort(list->list, list->size(), sizeof(String**), compare);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文