gpt4 book ai didi

c++ - C++ 中的比较器和优先级队列

转载 作者:行者123 更新时间:2023-11-30 02:58:37 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Can inner classes access private variables?

所以我尝试使用优先级队列,在这个队列的上下文中,如果 D[i] < D[j],我想将一个整数 i 定义为“小于”另一个整数 j。我怎样才能做到这一点? (D是对象的数据成员)

目前为止

/* This function gets the k nearest neighbors for a user in feature
* space. These neighbors are stored in a priority queue and then
* transferred to the array N. */
void kNN::getNN() {
int r;
priority_queue<int, vector<int>, CompareDist> NN;

/* Initialize priority queue */
for (r = 0; r < k; r++) {
NN.push(r);
}

/* Look at the furthest of the k users. If current user is closer,
* replace the furthest with the current user. */
for (r = k; r < NUM_USERS; r++) {
if (NN.top() > r) {
NN.pop();
NN.push(r);
}
}

/* Transfer neighbors to an array. */
for (r = 0; r < k; r++) {
N[r] = NN.top();
NN.pop();
}
}

在 kNN.hh 中:

class kNN {

private:
struct CompareDist {
bool operator()(int u1, int u2) {
if (D[u1] < D[u2])
return true;
else
return false;
}
};
...

但是,这给了我错误

kNN.hh: In member function ‘bool kNN::CompareDist::operator()(int, int)’:
kNN.hh:29: error: invalid use of nonstatic data member ‘kNN::D’

我该怎么办?如果我在比较器中引用特定对象,C++ 似乎不喜欢它,但我不知道如何在不引用 D 的情况下解决这个问题。

谢谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com