[Boost-bugs] [Boost C++ Libraries] #5227: find_ptr wrapper for map::find

Subject: [Boost-bugs] [Boost C++ Libraries] #5227: find_ptr wrapper for map::find
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-02-24 16:44:08


#5227: find_ptr wrapper for map::find
----------------------------------+-----------------------------------------
 Reporter: olafvdspek@… | Owner: no-maintainer
     Type: Feature Requests | Status: new
Milestone: Boost 1.47.0 | Component: utility
  Version: Boost 1.46.0 | Severity: Problem
 Keywords: |
----------------------------------+-----------------------------------------
 Hi,

 Would it be possible to include this handy wrapper for (unordered)
 map::find?

 {{{
 #include <boost/unordered_map.hpp>
 #include <iostream>
 #include <string>

 using namespace std;

 template <class T, class U>
 typename T::mapped_type* find_ptr(T& c, U v)
 {
         typename T::iterator i = c.find(v);
         return i == c.end() ? NULL : &i->second;
 }

 template <class T, class U>
 const typename T::mapped_type* find_ptr(const T& c, U v)
 {
         typename T::const_iterator i = c.find(v);
         return i == c.end() ? NULL : &i->second;
 }

 int main()
 {
   typedef boost::unordered_map<int, string> 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 << i->second << "\n";

   if (very_long_type_name_t::mapped_type* i = find_ptr(very_long_name, 1))
     cout << *i << "\n";

   /*
   very_long_type_name_t::iterator i = very_long_name.find(1); if (i !=
 very_long_name.end()) cout << i->second << "\n";
   if (very_long_type_name_t::mapped_type* i = find_ptr(very_long_name, 1))
 cout << *i << "\n";

   auto i = very_long_name.find(1); if (i != very_long_name.end()) cout <<
 i->second << "\n";
   if (auto i = find_ptr(very_long_name, 1)) cout << *i << "\n";
   */
 }
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5227>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:05 UTC