Originally Posted by
iMalc
A std::set never uses operator == or operator != for anything. The datatype you are using need not even supply such operators.
It knows that two items A and B are equal when A < B and B < A are both false.
What you probably want is:
Code:
typedef std::set<node>::iterator it;
This seems to work, but spawns more questions:
1) Now, what category is this iterator? Is it a random-access one, as I want it?
2) Reminding this:
Code:
struct node{
UL v,l;//(V)alue, (L)eader (of branch in Kruskal's algorithm)
node(UL x):v(x),l(x){}//Constructor
booloperator<(node x) {return v<x.v;}
};
typedef set<node>::iterator it;
struct edge{
it n1,n2;//Iterator(pointer) to element of node set (that is, a node)
double w;//weight
bool MSTE;
edge(it x, it y, double z): n1(x), n2(y), w(z),MSTE(false){}//Constructor
booloperator<(edge);//n1 will be the same in a set of edges under a specific node.
};
I wanted the edges to be sorted according to the nodes their iterators were pointing to. I tried:
Code:
bool edge::operator<(edge x)
{
if(n1==x.n1) return *n1<*x.n1;
elsereturn *n2<*x.n2;
but failed. Then I used this:
Code:
bool edge::operator<(edge x)
{
if(n1==x.n1)
returnconst_cast<node&>(*n1)<const_cast<node&>(*x.n1);
else
returnconst_cast<node&>(*n1)<const_cast<node&>(*x.n1);
}
Why did I need this cast?
3)
Code:
typedefmap<node,set<edge>> graph;
graph G;
node n1=...;
it it1;
it1=G.find(n1);
...won't compile with error:
"
1>d:\ha\structures and algorithms\project2_stl\main.cpp(31): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Tree_iterator<_Mytree>' (or there is no acceptable conversion)
"
why?
4) I don't lack whitespace, but when I paste text here, or even when I try to make it better, things get really messy. I'm using IE, is that the problem?