【发布时间】:2021-08-25 15:53:30
【问题描述】:
我有一个std::vector<std::vector<type T>> matrix,我将type T 的元素插入到这个矩阵中,我对这些元素逐行执行一些指令。我还需要在每次迭代时以最低成本删除元素。
我创建了一个std::priority_queue<Costelement, std::vector<Costelement>, Costelement::Comparekeys> Costvec;,其中:
struct Costelement
{
int row;
int column;
std::vector<double> cost;
struct CompareCosts
{
bool operator()(const Costelement &e1, const Costelement &e2)
{
return (e1.cost > e2.cost);
}
};
};
其中row 和column 是元素在matrix 中具有相应成本的位置。但是,当我从matrix 中删除具有最小键的元素时,相应row 中元素的位置会发生变化。我在matrix 的每次迭代中都使用了std::min_element,但这非常昂贵。我们如何才能有效地建模这种情况?
【问题讨论】:
-
为什么
Costelement结构的cost成员是double值的向量?不应该是一个double吗? -
在
e1.cost > e2.cost中,Costelement::cost是一个向量,比较如何工作?
标签: c++ vector min priority-queue multimap