Boost C++ Libraries: Ticket #5227: find_ptr wrapper for map::find https://svn.boost.org/trac10/ticket/5227 <p> Hi, </p> <p> Would it be possible to include this handy wrapper for (unordered) map::find? </p> <pre class="wiki">#include &lt;boost/unordered_map.hpp&gt; #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; template &lt;class T, class U&gt; typename T::mapped_type* find_ptr(T&amp; c, U v) { typename T::iterator i = c.find(v); return i == c.end() ? NULL : &amp;i-&gt;second; } template &lt;class T, class U&gt; const typename T::mapped_type* find_ptr(const T&amp; c, U v) { typename T::const_iterator i = c.find(v); return i == c.end() ? NULL : &amp;i-&gt;second; } int main() { typedef boost::unordered_map&lt;int, string&gt; very_long_type_name_t; very_long_type_name_t very_long_name; very_long_name[1] = "one"; very_long_type_name_t::iterator i = very_long_name.find(1); if (i != very_long_name.end()) cout &lt;&lt; i-&gt;second &lt;&lt; "\n"; if (very_long_type_name_t::mapped_type* i = find_ptr(very_long_name, 1)) cout &lt;&lt; *i &lt;&lt; "\n"; /* very_long_type_name_t::iterator i = very_long_name.find(1); if (i != very_long_name.end()) cout &lt;&lt; i-&gt;second &lt;&lt; "\n"; if (very_long_type_name_t::mapped_type* i = find_ptr(very_long_name, 1)) cout &lt;&lt; *i &lt;&lt; "\n"; auto i = very_long_name.find(1); if (i != very_long_name.end()) cout &lt;&lt; i-&gt;second &lt;&lt; "\n"; if (auto i = find_ptr(very_long_name, 1)) cout &lt;&lt; *i &lt;&lt; "\n"; */ } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5227 Trac 1.4.3 Marshall Clow Thu, 15 Mar 2012 18:30:10 GMT <link>https://svn.boost.org/trac10/ticket/5227#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:1</guid> <description> <p> How about declaring the variable in the if statement? </p> <pre class="wiki">if (auto i = very_long_name.find(1) != very_long_name.end()) cout &lt;&lt; i-&gt;second; } </pre><p> That makes the code simpler. I'm not dismissing the suggestion for a wrapper; just looking at alternatives as well. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Thu, 15 Mar 2012 18:33:51 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5227#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:2</guid> <description> <p> Yeah, except that I is now a bool. ;) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Thu, 15 Mar 2012 18:41:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5227#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:3</guid> <description> <p> Dang. Apparently the compiler in my head is defective. I need an upgrade. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 03 Jun 2012 20:22:54 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5227#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:4</guid> <description> <p> Are you sure it is safe to return &amp;i-&gt;second? What about returning optional&lt;typename T::mapped_type&gt;? </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Sun, 03 Jun 2012 21:25:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5227#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:5</guid> <description> <p> Yes, I am. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Mon, 02 Jul 2012 17:24:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5227#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:6</guid> <description> <p> To avoid issues with ptr_container, T::value_type::second_type should be used instead of T::mapped_type </p> <pre class="wiki">template &lt;class T, class U&gt; typename T::value_type::second_type* find_ptr(T&amp; c, U v) { typename T::iterator i = c.find(v); return i == c.end() ? NULL : &amp;i-&gt;second; } template &lt;class T, class U&gt; const typename T::value_type::second_type* find_ptr(const T&amp; c, U v) { typename T::const_iterator i = c.find(v); return i == c.end() ? NULL : &amp;i-&gt;second; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Mon, 09 Jul 2012 17:31:21 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5227#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:7</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/79385" title="Find wrappers for map/multimap; suggested by Olaf; Refs #5227; No docs yet.">[79385]</a>) Find wrappers for map/multimap; suggested by Olaf; Refs <a class="new ticket" href="https://svn.boost.org/trac10/ticket/5227" title="#5227: Feature Requests: find_ptr wrapper for map::find (new)">#5227</a>; No docs yet. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Mon, 09 Jul 2012 17:32:03 GMT</pubDate> <title>owner, component changed https://svn.boost.org/trac10/ticket/5227#comment:8 https://svn.boost.org/trac10/ticket/5227#comment:8 <ul> <li><strong>owner</strong> changed from <span class="trac-author">No-Maintainer</span> to <span class="trac-author">Marshall Clow</span> </li> <li><strong>component</strong> <span class="trac-field-old">utility</span> → <span class="trac-field-new">algorithm</span> </li> </ul> Ticket Marshall Clow Mon, 09 Jul 2012 17:32:24 GMT milestone changed https://svn.boost.org/trac10/ticket/5227#comment:9 https://svn.boost.org/trac10/ticket/5227#comment:9 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.47.0</span> → <span class="trac-field-new">Boost 1.52.0</span> </li> </ul> Ticket Olaf van der Spek <olafvdspek@…> Wed, 10 Oct 2012 09:32:28 GMT <link>https://svn.boost.org/trac10/ticket/5227#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:10</guid> <description> <p> Did this make it into 1.52? Can't find it. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Wed, 09 Jan 2013 14:28:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5227#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5227#comment:11</guid> <description> <p> Marshall? </p> </description> <category>Ticket</category> </item> </channel> </rss>