Note that there are some explanatory texts on larger screens.

plurals
  1. POSyntax for finding structs in multisets - C++
    text
    copied!<p>I can't seem to figure out the syntax for finding structs in containers.</p> <p>I have a multiset of <code>Event</code> structs. I'm trying to find one of these structs by searching on its key. I get the compiler error commented below.</p> <pre><code>struct Event { public: bool operator &lt; ( const Event &amp; rhs ) const { return ( time &lt; rhs.time ); } bool operator &gt; ( const Event &amp; rhs ) const { return ( time &gt; rhs.time ); } bool operator == ( const Event &amp; rhs ) const { return ( time == rhs.time ); } double time; int eventID; int hostID; int s; }; typedef std::multiset&lt; Event, std::less&lt; Event &gt; &gt; EventPQ; EventPQ currentEvents; double oldRecTime = 20.0; EventPQ::iterator ceItr = currentEvents.find( EventPQ::key_type( oldRecTime ) ); // no matching function call </code></pre> <p>I've tried a few permutations to no avail. I thought defining the conditional equality operator was going to be enough.</p> <hr> <p><strong>Solution</strong></p> <p>After correcting my typo (sorry), I now have a solution closest to AraK's, augmented by Soapbox's suggested use of <code>explicit</code>:</p> <pre><code>struct Event { explicit Event(double t) : time(t), eventID(), hostID(), s() {} Event(double t, int eid, int hid, int stype) : time(t), eventID( eid ), hostID( hid ), s(stype) {} ... }; EventPQ::iterator ceItr = currentEvents.find( EventPQ::key_type( Event(oldRecTime) ) ); </code></pre> <p>I recently discovered that another option would have been to use <code>find_if</code>, discussed <a href="https://stackoverflow.com/questions/589985/vectors-structs-and-stdfind">here</a>.</p> <p>Thanks for the help.</p>
 

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