- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试通过重载“+”运算符将两个优先级队列相互添加。
// Overloading add operator (+)
template <class T> PrioQueue<T> operator + (PrioQueue<T> &a, PrioQueue<T> &b) {
// Create a new queue that's big enough to hold our 2 queues
PrioQueue<T> newPrioQueue(a.num_items + b.num_items);
// Push all items to the new queue
// all items will be sorted on pushtime (get it? Instead of runtime? Haha... Ha...)
for (T * element = a.bottom_; element < a.bottom_ + a.num_items; element++) {
newPrioQueue.push(*element);
std::cout << newPrioQueue;
}
// Push all items to the new queue
// all items will be sorted on pushtime
for (T * element = b.bottom_; element < b.bottom_ + b.num_items; element++) {
newPrioQueue.push(*element);
std::cout << newPrioQueue;
}
// This one is needed - why?
newPrioQueue.sort();
std::cout << newPrioQueue;
// New queue is done, return it.
return newPrioQueue;
}
这是我的 PrioQueue 的 push
函数:
void push(T t) {
if (!full()) {
*top_ = t;
top_++;
num_items++;
// Sort items using qsort
qsort(bottom_, num_items, sizeof(T), compareFunction);
} else {
std::cout << "Queue is full" << endl;
}
};
如您所见,PrioQueue 将在每次push
后进行排序。但是,在合并两个数组后还需要最后一种排序!检查此输出:
Queue has 1 item(s): 429
Queue has 2 item(s): 429|123
Queue has 3 item(s): 429|123|24
Queue has 4 item(s): 429|123|24|24
Queue has 5 item(s): 429|123|24|24|3
Queue has 6 item(s): 429|123|24|24|3|2
Queue has 7 item(s): 24|123|24|429|3|2|1
Queue has 7 item(s): 429|123|24|24|3|2|1
倒数第二行是错误。它突然打乱了所有数据!
为什么会这样?
编辑:这是我的 CompareFunction:
static int compareFunction (const void * a, const void * b) {
// Relies on overloading of the "<" operator with non-std objects
return (*(T*)a < *(T*)b);
}
这是输入:
intQueue1.push(24);
intQueue1.push(429);
intQueue1.push(123);
intQueue2.push(1);
intQueue2.push(2);
intQueue2.push(3);
intQueue2.push(24);
intQueue3 = intQueue1 + intQueue2;
最佳答案
我可能是错的,但是你的compareFunction
:
static int compareFunction (const void * a, const void * b) {
// Relies on overloading of the "<" operator with non-std objects
return (*(T*)a < *(T*)b);
}
应该返回 1、0 或 -1,它返回 true 或 false...
关于c++ - QSort 排序不正确,除非之后再多排序一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29556856/
我正在尝试对这个字符串列表进行排序:["a", "z", "b"]。所以答案应该是 ["a", "b", "z"]。但是,当我尝试使用 C 的 qsort() 时,没有任何 Action !我做错了什
我尝试使用 qsort 创建一些基本代码来对字符串数组进行排序,但根据 gdb,它在 qsort 中崩溃了: #include #include static int pcmp(const voi
我想使用 qsort(...) 对 2D int 数组进行排序,但我的比较函数似乎是错误的。 qsort的参数如下: qsort(void *base, size_t nitems, size_t s
我一直在寻找一种方法来对 C 中的数组子集进行排序,而无需将元素移动到临时数组并将它们复制回来。我可能对 qsort 理解不好,但我认为下面的代码应该可以工作: qsort(&my_struct_ar
我正在使用标准 c 库的 qsort 函数对组织在数组中的数百万个结构进行排序。我试图通过创建具有相同长度的结构指针数组来优化性能。与我的预期相反,第二个变体的执行时间较慢: qsort 结构数组:1
我成功地对结构数组进行了排序,其中每个结构仅包含一个 char 字符串。然而,我的问题是,对于大约的结构数组。 900,000 个元素,qsort 比我预期的要长很多(qsort 需要大约 2 分钟来
这个问题在这里已经有了答案: How to qsort an array of pointers to char in C? (8 个答案) 关闭 5 年前。 我尝试使用 qsort 对字符串数组进
我正在尝试重构一个当前是独立 C 程序的实用程序,以便我可以创建一个可重用的库。它包括根据全局数组中的相应值对数组进行排序的步骤。 // Global lookup table double *rat
我尝试对下面的一个struct进行排序,目的是对它们的错误率进行排序,同时保留sid和did的信息。虽然没有编译错误,但我在运行时遇到段错误。我想知道出了什么问题.... #include #inc
我正在尝试创建一个函数模板,该模板接收一个数组作为输入并对它进行排序。为了对其进行排序,我想我可以使用C++标准库的头文件qsort()中包含的cstdlib函数。 qsort()函数需要将比较器函数
#include #include #include int sortstring(const void *str1, const void *str2) { const char *r
我正在尝试对一个名为 results 的 struct run 数组进行排序,但是当我打印该数组时,没有任何排序。看看这个: struct run { char name[20], weekday
我尝试对下面的一个struct进行排序,目的是对它们的错误率进行排序,同时保留sid和did的信息。虽然没有编译错误,但我在运行时遇到段错误。我想知道出了什么问题.... #include #inc
考虑一个结构指针数组。以下代码取自您可能会找到的示例 here 。我想要为这两排铸件进行移植。我对这种“双重类型转换”不熟悉。 int myptrstructcmp(const void *p1, c
为什么我们在使用qsort()时,int(*compar)(const void*,const void*)没有添加任何参数,却也能完成它们的功能呢? 比如这样: double vals[NUM];
void qsort ( void* base, size_t num, size_t size, int (*compar)(const void*,const vo
嗨,我是学习 C 编程语言的新手,很难理解这个问题。我想对指向 struct[person] 指针数组的双指针进行排序。我需要根据多个标准对结构指针进行排序。 (年龄按升序排列,姓名和高度均按降序排列
我想对数组的特定列进行排序,但必须相应地移动其他元素例如 UNSORTED ARRAY 40 2 30 6 20 1 REQUIRED SORTED ARRAY 20 1 30 6 40 2 如果不能
假设我们有一个结构: struct product { char name[30]; float price; }; 我想首先使用 qsort 按价格对其进行排序,如果价
这个问题已经有答案了: What sorting algorithm does qsort use? (3 个回答) 已关闭 9 年前。 该功能是否qsort()在stdlib.h实际上使用快速排序算
我是一名优秀的程序员,十分优秀!