我很难打印出集合中的元素。
std::set<triple, Compare> edges;
for (int i = 0; i < n; i++)
for (std::list<std::pair<int, int>>::iterator j = graph[i].begin(); j != graph[i].end(); j++)
edges.insert(makeTriple((*j).second, i, (*j).first));
for (std::set<triple, Compare>::iterator j = edges.begin(); j != edges.end(); j++)
printf("%d and %d\n\n", (*j).first + 1, (*j).second + 1);
仅打印7个元素(共13个)。Compare函数如下:
bool operator()(const triple &a, const triple &b) const
{
if (a.distance == b.distance && a.first == b.first)
return (a.second < b.second);
if (a.distance == b.distance && a.second == b.second)
return (a.first < b.first);
return (a.distance < b.distance);
}
您的比较功能不满足要求
如果
Compare
在上下文调用中将调用的第一个参数以该类型所引发的严格弱排序关系中的第二个参数之前的形式出现,则在上下文转换为时,应用于类型满足的对象的函数调用操作的返回值将bool
产生。true
false
最明显的解决办法是:
bool operator()(const triple &a, const triple &b) const {
if (a.distance == b.distance) {
if(a.first == b.first) return a.second < b.second;
else return a.first < b.first;
} else return a.distance < b.distance;
}
但是,可以使用来自以下位置的std :: tie来简化此操作<tuple>
bool operator()(const triple &a, const triple &b) const {
return
std::tie(a.distance, a.first, a.second) < std::tie(b.distance, b.first, b.second);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句