Opened 7 years ago

Last modified 6 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 Changed 6 years ago by Marshall Clow

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 Changed 6 years ago by Olaf van der Spek <olafvdspek@…>

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

comment:3 Changed 6 years ago by Marshall Clow

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

comment:4 Changed 6 years ago by viboes

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

comment:5 Changed 6 years ago by Olaf van der Spek <olafvdspek@…>

Yes, I am.

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

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 Changed 6 years ago by Marshall Clow

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

comment:8 Changed 6 years ago by Marshall Clow

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

comment:9 Changed 6 years ago by Marshall Clow

Milestone: Boost 1.47.0Boost 1.52.0

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

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

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

Marshall?

Note: See TracTickets for help on using tickets.