I have trouble printing out elements in set.
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);
Only 7 elements are printed of of 13. Compare function looks like:
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);
}
Your Compare function does not fulfill the requirement
The return value of the function call operation applied to an object of a type satisfying
Compare
, when contextually converted tobool
, yieldstrue
if the first argument of the call appears before the second in the strict weak ordering relation induced by this type, andfalse
otherwise.
The most obvious fix would be:
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;
}
This can however be done simpler with std::tie from <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);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments