Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using bind2nd function on map

using bind2nd function on map

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fordge
    wrote on last edited by
    #1

    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???

    C 1 Reply Last reply
    0
    • F fordge

      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???

      C Offline
      C Offline
      CP Visitor
      wrote on last edited by
      #2

      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?

      F 1 Reply Last reply
      0
      • C CP Visitor

        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?

        F Offline
        F Offline
        fordge
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups