- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 std::stable_sort
时一直遇到类型问题我不断收到错误消息:
argument of type 'bool (Memory::)(const Mem&, const Mem&)' does not match 'bool (Memory::*)(const Mem&, const Mem&)'
我不明白为什么它显示为指针...如果有人能看一下,将不胜感激。
内存.cpp:
#include <vector>
#include <algorithm>
#include <iostream>
#include <set>
#include "Memory.h"
#include "constants.hpp"
Memory::Memory(int _type) {
fit_type = _type;
blocks = 1;
mem[0].address = 0;
mem[0].size = 2048;
mem[0].item_id = EMPTY;
}
void Memory::sort_mem_list() {
// Sort by address
std::stable_sort(mem.begin(), mem.end(), Memory::compareByAddress );
}
bool Memory::compareByAddress(const Mem &a, const Mem &b) {
return a.address < b.address;
}
和内存.hpp
#ifndef MEMORY_H_
#define MEMORY_H_
#include <vector>
#include "process.h"
#include "constants.hpp"
class Memory {
public:
Memory(int);
int add_item(Process pr);
void remove_item();
void sort_mem_list();
void set_fit_type(int);
void memory_dump();
private:
bool compareBestFit(const Mem & a, const Mem & b);
bool compareWorstFit(const Mem & a, const Mem & b);
bool compareByAddress(const Mem & a, const Mem & b);
bool compareByProcess(const Mem & a, const Mem & b);
int fit_type;
int memory_size;
int blocks;
std::vector<Mem> mem;
};
#endif /* MEMORY_H_ */
内存(当前在 constants.hpp 中)
struct Mem {
int address;
int size;
int item_id;
Mem() {
address = 0;
size = 0;
item_id = EMPTY;
}
Mem(int a, int b, int c) {
address = a;
size = b;
item_id = c;
}
};
我确信这是相当简单的事情,我只是以某种方式弄乱了声明,但我已经坚持了一段时间,所以第二双眼睛会很有帮助。
最佳答案
如果您计划使用成员函数作为传递给 std::stable_sort()
的比较器,它需要是一个static
函数。
关于c++ - 类内的 stable_sort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780150/
我在使用 std::stable_sort 时一直遇到类型问题我不断收到错误消息: argument of type 'bool (Memory::)(const Mem&, const Mem&)'
我想对 int32_t 数组进行排序,使得所有正数(包括零)首先出现,然后是所有负数。输出中 +ve 和 -ve 数字的相对顺序应与输入的相对顺序相同。例如。 Input : 9, 4, -2, -
我有一个 std::vector of Object,Object 可以是 Empty 或 NotEmpty(这是一个内部状态,Object 本身总是有效的)。 我想按内部状态对 vector 进行排
在标量类型(即 int、long、等)与默认比较器? 如果是这样,你应该什么时候做? 如果不是,那么为什么标准库不直接将此类调用转发给 sort?那不是更快吗? 最佳答案 稳定排序仅在您排序的项目具有
这page告诉我们,每当内存不足时,stable_sort 都会缩减为运行时间为 O(n(log n)(log n)) 的就地算法: Complexity If enough extra memory
我正在使用 C++ stable_sort 使用比较器函数按升序对我的类对象的 vector 进行排序,但排序不稳定。一种可行的解决方法是反向迭代并反转比较器中的逻辑。但不明白为什么它不应该正常工作。
各位程序员,您好,我正在解决有关leetcode的问题,该问题需要稳定排序。 问题链接:https://leetcode.com/problems/rearrange-words-in-a-sente
我已经定义了一个结构 ABC 来包含一个 int ID、字符串 NAME、字符串 LAST_NAME; 我的程序是这样的:从输入文件中读取一行。将每一行解析为名字和姓氏,并插入到 ABC 结构中。此外
我在使用 std::stable_sort 时遇到了一个奇怪的错误在 Point 上数据类型。默认排序 Point值首先由 x 坐标确定,然后由 y 坐标确定以打破平局。但是,我正在使用 std::s
#include #include #include #include using namespace std; bool foo(string&s1, string&s2); int mai
我称之为 bool cmpNAME(const PlayerCard& a, const PlayerCard& b){ return (a.Name tmp; //fill tmp sta
我希望使用 std::stable_sort。算法的复杂度表示为 O(N·log^2(N)), where N = std::distance(first, last) applications of
通过阅读《C++ sort()排序函数》一节,读者已经了解了 sort() 函数的功能和用法。值得一提的是,当指定范围内包含多个相等的元素时,sort() 排序函数无法保证不改变它们的相对位置。那么,
我有以下简单程序。在 test1 和 test2 中,我尝试对 2 个字符串“2”和“1”进行排序,在下面的示例中,函数 compare 将始终返回错误。 #include #include #i
如果我将 lambda 作为引用参数传递给排序算法,它就可以正常工作。但是当我将相同的 lambda 传递给 stable_sort 时,它没有编译,我必须声明参数 const?为什么会这样? //
struct SimGenRequest { int wakeup_mfm_; double value_; bool operator #include #include
我有 class Passanger有变量 string name; string station; string ticket;然后我有另一个类,在这个类中我有 vector myQueue; 现在
这是文档: val sort : ('a -> 'a -> int) -> 'a list -> 'a list 根据比较函数对列表进行升序排序。如果比较函数的参数比较相等,则比较函数必须返回 0,如
这个问题在这里已经有了答案: What causes std::sort() to access address out of range (2 个回答) 1年前关闭。 https://leetcod
我有大量整数需要排序。这里要注意的是,列表中的每个条目都包含许多其他关联元素,这些元素在排序时需要跟在该 int 周围。我已经通过将排序更改为对 double 而不是整数进行排序来解决这个问题。我在每
我是一名优秀的程序员,十分优秀!