Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The definition of the "ID" in the question isn't really clear: do you need to use it as a key for fast Vertex lookup? You could define a comparator for the <code>std::map</code> (see below for an example)</p> <p>Do you need to be able to differentiate between two Vertex objects with the same coordinates (but different in another field)? Define some 'id factory' (cfr. the singleton pattern) that generates e.g. a sequence of ints, unrelated to the values of the Vertex objects. - Much in the way Fire Lancer suggests (but beware of thread-safety issues!)</p> <p>In my opinion, two vertices with identical coordinates are identical. So why would you even need an extra ID?</p> <p>As soon as you define a '<a href="http://en.wikipedia.org/wiki/Strict_weak_ordering" rel="nofollow noreferrer">strict weak ordering</a>' on this type, you can use it as a key in e.g. an <code>std::map</code>, </p> <pre><code>struct Vertex { typedef short int Value; Value v1, v2; bool operator&lt;( const Vertex&amp; other ) const { return v1 &lt; other.v1 || ( v1 == other.v1 &amp;&amp; v2 &lt; other.v2 ) ; }; Vertex x1 = { 1, 2 }; Vertex x2 = { 1, 3 }; Vertex y1 = { 1, 2 }; // too! typedef std::set&lt;Vertex&gt; t_vertices; t_vertices vertices; vertices.insert( x1 ); vertices.insert( x2 ); vertices.insert( y1 ); // won't do a thing since { 1, 2 } is already in the set. typedef std::map&lt;Vertex, int&gt; t_vertex_to_counter; t_vertex_to_counter count; count[ x1 ]++; assert( count[x1] == 1 ); assert( count[y1] == 1 ); count[ x2 ]++; count[ y1 ]++; assert( count[x1] == 2 ); assert( count[y1] == 2 ); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload