Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>The first question:</strong></p> <p>Since you are using the <code>unordered_multiset_of</code> collection type for the right side of your bimap type, it means that it will have an interface compatible with <code>std::unordered_multimap</code>. <code>std::unordered_multimap</code> has the member function <code>equal_range(const Key&amp; key)</code> which returns a <code>std::pair</code> of iterators, one pointing to the first element that has the desired key and one that points to one past the end of the range of elements that have the same key. Using that you can iterate over the range with the matching key without comparing the key to the value in the iteration condition.</p> <p>See <a href="http://www.boost.org/doc/libs/1_41_0/libs/bimap/doc/html/boost_bimap/the_tutorial/controlling_collection_types.html" rel="nofollow">http://www.boost.org/doc/libs/1_41_0/libs/bimap/doc/html/boost_bimap/the_tutorial/controlling_collection_types.html</a> and <a href="http://en.cppreference.com/w/cpp/container/unordered_multimap/equal_range" rel="nofollow">http://en.cppreference.com/w/cpp/container/unordered_multimap/equal_range</a> for references.</p> <p><strong>The second question:</strong></p> <p>Constructing a list or other actual container of pointers or references to the elements with the matching values and returning that is inefficient since it's always going to require O(n) space, whereas just letting the user iterate over the range in the original bimap only requires returning two iterators, which only require O(1) memory.</p> <p>You can either write a member function that returns the iterators directly, e.g.</p> <pre><code>typedef PointMap::right_const_iterator match_iterator; std::pair&lt;match_iterator, match_iterator&gt; GetPoints(Value v) { return point_map.right.equal_range(v); } </code></pre> <p>or you can write a proxy class that presents a container-like interface by having begin() and end() member functions returning those two iterators, and have your <code>GetPoints()</code> member function return an object of that type:</p> <pre><code>class MatchList { typedef PointMap::right_const_iterator iterator; std::pair&lt;iterator, iterator&gt; m_iters; public: MatchList(std::pair&lt;iterator, iterator&gt; const&amp; p) : m_iters(p) {} MatchList(MatchList const&amp;) = delete; MatchList(MatchList&amp;&amp;) = delete; MatchList&amp; operator=(MatchList const&amp;) = delete; iterator begin() { return m_iters.first; } iterator end() { return m_iters.second; } }; </code></pre> <p>It's a good idea to make it uncopyable, unmovable and unassignable (like I've done above by deleting the relevant member functions) since the user may otherwise keep a copy of the proxy class and try to access it later when the iterators could be invalidated.</p> <p>The first way means writing less code, the second means presenting a more common interface to the user (and allows for hiding more stuff in the proxy class if you need to modify the implementation later).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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