using bind2nd function on map
-
suppose i have a map m; if i need to search for the location using the int data im currently doing map::iterator iter; for(iter=m.begin(); iter !=m.end(); iter++) { if(iter->second == 10) break; } can i replace this with something like this iter = find_if(m.begin(),m.end(),bind2nd(KeyEquals(),10)); in which case what would the KeyEquals function object be like???
-
suppose i have a map m; if i need to search for the location using the int data im currently doing map::iterator iter; for(iter=m.begin(); iter !=m.end(); iter++) { if(iter->second == 10) break; } can i replace this with something like this iter = find_if(m.begin(),m.end(),bind2nd(KeyEquals(),10)); in which case what would the KeyEquals function object be like???
fordge wrote: _suppose i have a map m; if i need to search for the location using the int data im currently doing
map::iterator iter;
for(iter=m.begin(); iter !=m.end(); iter++)
{
if(iter->second == 10)
break;
}can i replace this with something like this
iter = find_if(m.begin(),m.end(),bind2nd(KeyEquals(),10));
in which case what would the KeyEquals function object be like???_ Sure you can in some way. But why would you want to do this? You have a simple, maintainable solution. Why replace it with a geek-style, Boost-ish hack?
-
fordge wrote: _suppose i have a map m; if i need to search for the location using the int data im currently doing
map::iterator iter;
for(iter=m.begin(); iter !=m.end(); iter++)
{
if(iter->second == 10)
break;
}can i replace this with something like this
iter = find_if(m.begin(),m.end(),bind2nd(KeyEquals(),10));
in which case what would the KeyEquals function object be like???_ Sure you can in some way. But why would you want to do this? You have a simple, maintainable solution. Why replace it with a geek-style, Boost-ish hack?
in this case it may be geekish yes..but i still find it more readable but there may be more generic adapters where it would be better to use something other than that while loop also most of the bind1st n bind2nd articles deal only with vectors and was finding it hard to reproduce the same on maps and sets anyway here is the solution i got from a fellow coder typedef std::map MyMap; bool KeyEquals(MyMap::value_type value, int DataValue) { return value.second == DataValue; } std::string blah(MyMap const& theMap, int thing) { MyMap::const_iterator it = std::find_if(theMap.begin(), theMap.end(), std::bind2nd(std::ptr_fun(KeyEquals), thing)); return (it==theMap.end())?std::string():it->second; }