Opened 12 years ago
Last modified 11 years ago
#5342 new Feature Requests
find_ptr (find wrapper)
Reported by: | Owned by: | Thorsten Ottosen | |
---|---|---|---|
Milestone: | Boost 1.47.0 | Component: | ptr_container |
Version: | Boost 1.46.0 | Severity: | Problem |
Keywords: | Cc: |
Description
Could you add a find wrapper to ptr (unordered) map that returns mapped_type* instead of an iterator (and NULL if not found)?
The same could be provided for the other containers, but there it's less useful.
Change History (7)
comment:1 by , 12 years ago
comment:2 by , 12 years ago
Hi Thorsten,
Why at()? at() returns a reference while the requested functionality is meant as a replacement for find().
Anyway, this should be hard to code a small function that does what you want
It's easy, indeed. But it's so useful that IMO it should be part of the lib itself.
comment:3 by , 12 years ago
Actually, a find_ref() would be nice too. It'd return a reference and require the key to exist.
comment:4 by , 11 years ago
You're right, this can be done with non-member functions:
template <class T, class U> typename T::value_type::second_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::value_type::second_type* find_ptr(const T& c, U v) { typename T::const_iterator i = c.find(v); return i == c.end() ? NULL : &i->second; } template <class T, class U> typename T::value_type::second_type find_ptr2(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::value_type::second_type find_ptr2(const T& c, U v) { typename T::const_iterator i = c.find(v); return i == c.end() ? NULL : i->second; }
Now the hard part is to find a Boost lib that'd like to adopt these functions.
Hi
what about
?
Anyway, this should be hard to code a small function that does what you want, so I'm not too happy about adding this.
-Thorsten