C++ 类内的稳定排序
在使用C++ 类内的稳定排序,c++,sorting,struct,stable-sort,C++,Sorting,Struct,Stable Sort,在使用std::stable\u sort 我不断得到错误: argument of type 'bool (Memory::)(const Mem&, const Mem&)' does not match 'bool (Memory::*)(const Mem&, const Mem&)' 我不明白为什么它会以指针的形式出现……如果有人能看一看,我将不胜感激 Memory.cpp: #include <vector> #include <a
std::stable\u sort
我不断得到错误:
argument of type 'bool (Memory::)(const Mem&, const Mem&)' does not match 'bool (Memory::*)(const Mem&, const Mem&)'
我不明白为什么它会以指针的形式出现……如果有人能看一看,我将不胜感激
Memory.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;
}
我确信这是一件相当简单的事情,我只是把声明弄得一团糟,但我已经坚持了一段时间,所以第二组眼睛将非常有用。如果您计划使用成员函数作为传递给
std::stable_sort()的比较器
它必须是一个静态
函数。将&
粘贴在前面,传递指向函数的指针非常感谢!我知道这是一个愚蠢的编译器在叫喊:你在传递我bool(内存::)(const-Mem&,const-Mem&)
,但我要了一个指针bool(内存::*
)(const-Mem&,const-Mem&)——这是编译器在跟你说话;-)如果你发现自己花了太多的时间却没有什么结果,那么试着散散步或者用其他方式分散你的注意力。我的百分之二看起来你需要一个“橡皮鸭”来解释你的问题,这样你就能自己看到问题所在了
#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_ */
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;
}
};