有趣的问题。后一部分(基于映射源键值的优先级顺序)提出了真正的挑战。
映射自定义键比较
从键比较到映射的基本三种方法是:
- 为映射键类型提供
operator < 成员覆盖,或者
- 提供函子类型作为地图的比较器,或者
- 提供免费功能。
其中最常见的是第一个,因为它最容易实现和可视化。映射的默认比较器是std::less<K>,其中K 是键类型。标准库std::less 默认尝试进行operator < 比较,实际上它是这样做的:
bool isLess = (a < b)
其中a 和b 都是您的密钥类型。因此,一个简单的成员 const operator < 重载将满足要求并为您提供所需的内容:
struct Node {
Node(int a=0, int b=0)
: x(a), y(b)
{
}
int x, y;
// called by default std::less
bool operator <(const Node& rhs) const
{
return (x < rhs.x) || (!(rhs.x < x) && y < rhs.y);
}
// simple distance calculation between two points in 2D space.
double distanceFrom(Node const& node) const
{
return std::sqrt(std::pow((x - node.x), 2.0) + std::pow((y - node.y), 2.0));
}
friend std::ostream& operator <<(std::ostream& os, Node const& node)
{
return os << '(' << node.x << ',' << node.y << ')';
}
};
这支持严格的弱顺序并且就足够了。其他选项有点复杂,但不是很多。我不会在这里介绍它们,但是有很多关于 SO 的问题。
注意:我添加了distanceFrom 和operator << 成员和朋友,以供以后在最终示例中使用;稍后您会看到它们。
优先队列实例比较覆盖
以前从未这样做过,但如果有更简单的方法,我当然愿意接受建议。为您的优先级队列使用模板类型的比较器覆盖的问题是您不能真正做到这一点。您希望根据到原点的距离对每个队列进行排序,其中原点是该队列的映射键。这意味着必须以某种方式为每个 comparison 对象提供映射键的来源,而您不能通过模板类型覆盖(即编译时的东西)来做到这一点。
然而,您可以做的是提供一个实例比较覆盖。 std::priority_queue 允许您提供自定义比较对象在哪里创建队列(在我们的例子中,当它作为映射目标插入到地图中时)。稍微按摩一下,我们想出了这个:
首先,一个不带参数或节点的优先级函子。
// instance-override type
struct NodePriority
{
NodePriority() = default;
NodePriority(Node node)
: key(std::move(node))
{
}
// compares distance to key of two nodes. We want these in
// reverse order because smaller means closer means higher priority.
bool operator()(const Node& lhs, const Node& rhs) const
{
return rhs.distanceFrom(key) < lhs.distanceFrom(key);
}
private:
Node key;
};
using NodeQueue = std::priority_queue<Node, std::deque<Node>, NodePriority>;
using NodeQueue 将为我们节省大量输入以下示例的时间。
示例
使用上述内容,我们现在可以构建地图和队列了。下面创建一个由十个节点组成的随机列表,每个节点的进位和 x,y 都在 1..9 的范围内。然后我们使用这些节点构建十个优先级队列,一个用于我们正在创建的每个映射条目。映射条目是对角线切片(即 (1,1)、(2,2)、(3,3) 等)。使用相同的十个随机元素,当我们报告最终结果时,我们应该会看到不同的优先级队列排序。
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <queue>
#include <map>
#include <random>
struct Node {
Node(int a=0, int b=0)
: x(a), y(b)
{
}
int x, y;
// called by default std::less
bool operator <(const Node& rhs) const
{
return (x < rhs.x) || (!(rhs.x < x) && y < rhs.y);
}
// simple distance calculation between two points in 2D space.
double distanceFrom(Node const& node) const
{
return std::sqrt(std::pow((x - node.x), 2.0) + std::pow((y - node.y), 2.0));
}
friend std::ostream& operator <<(std::ostream& os, Node const& node)
{
return os << '(' << node.x << ',' << node.y << ')';
}
};
// instance-override type
struct NodePriority: public std::less<Node>
{
NodePriority() = default;
NodePriority(Node node)
: key(std::move(node))
{
}
// compares distance to key of two nodes. We want these in
// reverse order because smaller means closer means higher priority.
bool operator()(const Node& lhs, const Node& rhs) const
{
return rhs.distanceFrom(key) < lhs.distanceFrom(key);
}
private:
Node key;
};
using NodeQueue = std::priority_queue<Node, std::deque<Node>, NodePriority>;
int main()
{
std::mt19937 rng{ 42 }; // replace with { std::random_device{}() } for random sequencing;
std::uniform_int_distribution<> dist(1, 9);
std::map<Node, NodeQueue> myMap;
// generate ten random points
std::vector<Node> pts;
for (int i = 0; i < 10; ++i)
pts.emplace_back(Node(dist(rng), dist(rng)));
for (int i = 0; i < 10; ++i)
{
Node node(i, i);
myMap.insert(std::make_pair(node, NodeQueue(NodePriority(node))));
for (auto const& pt : pts)
myMap[node].emplace(pt);
}
// enumerate the map of nodes and their kids
for (auto& pr : myMap)
{
std::cout << pr.first << " : {";
if (!pr.second.empty())
{
std::cout << pr.second.top();
pr.second.pop();
while (!pr.second.empty())
{
std::cout << ',' << pr.second.top();
pr.second.pop();
}
}
std::cout << "}\n";
}
}
注意:伪随机生成器始终以42 为种子,以具有可重复的序列。当您决定通过不可重复测试来放松这一点时,只需将该种子替换为声明旁边的注释中提供的种子即可。
输出(当然,您的输出会有所不同)。
(0,0) : {(3,1),(5,1),(3,5),(5,3),(5,4),(5,5),(1,9),(6,7),(5,8),(6,8)}
(1,1) : {(3,1),(5,1),(3,5),(5,3),(5,4),(5,5),(6,7),(1,9),(5,8),(6,8)}
(2,2) : {(3,1),(3,5),(5,1),(5,3),(5,4),(5,5),(6,7),(5,8),(1,9),(6,8)}
(3,3) : {(3,1),(3,5),(5,3),(5,4),(5,5),(5,1),(6,7),(5,8),(6,8),(1,9)}
(4,4) : {(5,4),(5,5),(3,5),(5,3),(5,1),(3,1),(6,7),(5,8),(6,8),(1,9)}
(5,5) : {(5,5),(5,4),(3,5),(5,3),(6,7),(5,8),(6,8),(5,1),(3,1),(1,9)}
(6,6) : {(6,7),(5,5),(6,8),(5,4),(5,8),(3,5),(5,3),(5,1),(1,9),(3,1)}
(7,7) : {(6,7),(6,8),(5,8),(5,5),(5,4),(3,5),(5,3),(1,9),(5,1),(3,1)}
(8,8) : {(6,8),(6,7),(5,8),(5,5),(5,4),(3,5),(5,3),(1,9),(5,1),(3,1)}
(9,9) : {(6,8),(6,7),(5,8),(5,5),(5,4),(3,5),(5,3),(1,9),(5,1),(3,1)}
我会让你验证 distanceFrom 计算的准确性,但希望你能明白。