Opened 12 years ago

Last modified 10 years ago

#5227 new Feature Requests

find_ptr wrapper for map::find

Reported by: olafvdspek@… Owned by: Marshall Clow
Milestone: Boost 1.52.0 Component: algorithm
Version: Boost 1.46.0 Severity: Problem
Keywords: Cc:

Description

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";
  */
}

Change History (11)

comment:1 by Marshall Clow, 11 years ago

How about declaring the variable in the if statement?

if (auto i = very_long_name.find(1) != very_long_name.end())
     cout << i->second;
}

That makes the code simpler. I'm not dismissing the suggestion for a wrapper; just looking at alternatives as well.

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

Yeah, except that I is now a bool. ;)

comment:3 by Marshall Clow, 11 years ago

Dang. Apparently the compiler in my head is defective. I need an upgrade.

comment:4 by viboes, 10 years ago

Are you sure it is safe to return &i->second? What about returning optional<typename T::mapped_type>?

comment:5 by Olaf van der Spek <olafvdspek@…>, 10 years ago

Yes, I am.

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

To avoid issues with ptr_container, T::value_type::second_type should be used instead of T::mapped_type

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;
}

comment:7 by Marshall Clow, 10 years ago

(In [79385]) Find wrappers for map/multimap; suggested by Olaf; Refs #5227; No docs yet.

comment:8 by Marshall Clow, 10 years ago

Component: utilityalgorithm
Owner: changed from No-Maintainer to Marshall Clow

comment:9 by Marshall Clow, 10 years ago

Milestone: Boost 1.47.0Boost 1.52.0

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

Did this make it into 1.52? Can't find it.

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

Marshall?

Note: See TracTickets for help on using tickets.