Opened 12 years ago

Last modified 11 years ago

#5342 new Feature Requests

find_ptr (find wrapper)

Reported by: olafvdspek@… 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 Thorsten Ottosen, 12 years ago

Hi

what about

map.at_ptr( key );

?

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

comment:2 by Olaf van der Spek <olafvdspek@…>, 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 Olaf van der Spek <olafvdspek@…>, 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 Olaf van der Spek <olafvdspek@…>, 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.

comment:5 by anonymous, 11 years ago

I think a natural place would be the pointer containers.

-Thorsten

comment:6 by Olaf van der Spek <olafvdspek@…>, 11 years ago

Why? It's useful for normal map and unordered_map too.

comment:7 by Olaf van der Spek <olafvdspek@…>, 11 years ago

Note: See TracTickets for help on using tickets.