Iterator i
can be used like a pointer to Position
, so you can write
Position visited = ...
for (set<Position>:iterator i = m_visited.begin(); i != m_visited.end(); i++) {
if (i->m_x == visited.m_x && i->m_y == visited.m_y) {
... // Been there before
}
}
However, this use of the set
is not much different from a use of a list
, because the serach is linear. You can find out if the item you have inserted is a new one without a loop by checking the return value of insert
:
if (!m_visited.insert(Position(CorX(),CorY())).second) {
// Position at { CorX(), CorY() } has been visited
}
set::insert
returns a pair
with the second member set to true
if the item that you have inserted is a new one, and to false
if another item with the same data is already in the set.