使用自定义类集时出错
Error using set of custom class
当我尝试编译以下代码时,我得到
'const elem' is not derived from 'const std::__cxx11::sub_match<_BiIter>'
我不明白这是什么意思,我的代码有什么问题?
struct elem
{
int x,y,val;
elem(int x,int y,int val)
{
this->x = x;
this->y = y;
this->val = val;
}
bool operator<(const elem b)
{
return val > b.val;
}
};
int kthSmallest(int v[MAX][MAX], int n, int k)
{
set<pair<int,int>> m;
priority_queue<elem, vector<elem>> pq;
int temp = k;
pq.emplace(0,0,v[0][0]);
while(temp != 0)
{
temp--;
if(pq.top().x + 1 < n && m.find(make_pair(pq.top().x + 1, pq.top().y)) == m.end())
{
m.insert(make_pair(pq.top().x + 1, pq.top().y));
pq.emplace(pq.top().x + 1, pq.top().y, v[pq.top().x + 1][pq.top().y]);
}
if(pq.top().y + 1 < n && m.find(make_pair(pq.top().x + 1, pq.top().y)) == m.end())
{
m.insert(make_pair(pq.top().x, pq.top().y + 1));
pq.emplace(pq.top().x, pq.top().y + 1, v[pq.top().x][pq.top().y + 1]);
}
}
return pq.top().val;
}
std::priority_queue
的第三个模板参数是 Compare
- 二进制运算符,默认情况下为 std::less。它需要两个参数作为const
。您的第一个参数 operator<
(默认情况下是调用它的对象)不const
,因为该方法未const
。这就是为什么您需要按照评论中指出的那样更改它的原因:
bool operator<(const elem b) const
{
return val > b.val;
}
相关文章:
- TraciMobility::getExternalId 将自定义模块添加到示例中时出错Veins_Inet (Omne
- 从 ParaView 构建自定义 Qt 应用程序示例时出错
- 从 Arduino 中的自定义C++库构造对象时出错
- 迭代器关系运算符出错(带单独链接和迭代器的自定义哈希表)
- 使用自定义类集时出错
- 在自定义库中使用正则表达式时出错
- C++ 使用带有变量作为参数的自定义 ifstream 类时出错
- 制作自定义 cout 时出错
- C++:将自定义哈希键值从 unordered_map 输出到 std::cout 时出错
- 返回对自定义对象的引用时 Boost.Python 出错
- 添加到方法的 C++ 自定义异常会出错
- 将自定义 Apache 2.4 模块与 Linux 上的 httpd/apr 库静态链接时出错
- 编译自定义容器以提升精神时出错
- 在 Windows 上使用 CMake 2.8.11 构建自定义 Qt 4.8.3 库时出错
- 自定义迭代器和 STL 算法出错
- 自定义结构集声明中出错
- 在b树的自定义迭代器中使用操作符*出错
- xcode中的c++:在main中调用自定义对象中的任何(!!!!)方法时出错
- 在Visual Studio中链接PostgreSQL自定义C++项目时出错
- 自定义排序元组向量时出错