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. ATL / WTL / STL
  4. using bind2nd function on map

using bind2nd function on map

Scheduled Pinned Locked Moved ATL / WTL / STL
question
4 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???

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

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Something like this should do the trick

      typedef std::map MyMap;
      
      bool KeyEquals(MyMap::value_type value, int keyValue)
      {
         return value.first == keyValue;
      }
      
      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;
      }
      

      PS - try out www.boost.org.Bind. This makes function binding a lot easier. You'd have the code below instead...

      std::string blah(MyMap const& theMap, int thing)
      {
         MyMap::const_iterator it = 
            std::find_if(theMap.begin(), theMap.end(), 
                         boost::bind(&KeyEquals, _1, thing));
         return (it==theMap.end())?std::string():it->second;
      }
      

      i.e. no std::ptr_fun, no having to say that you're binding the second parameter. You can see that the order of the parameters passed to bind matches the function signature order of KeyEquals, with '_1' (a predefined placeholder) denoting that the first (and only, in this case) parameter of the bound function object is the value passed by find_if. HTH! Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

      F 1 Reply Last reply
      0
      • S Stuart Dootson

        Something like this should do the trick

        typedef std::map MyMap;
        
        bool KeyEquals(MyMap::value_type value, int keyValue)
        {
           return value.first == keyValue;
        }
        
        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;
        }
        

        PS - try out www.boost.org.Bind. This makes function binding a lot easier. You'd have the code below instead...

        std::string blah(MyMap const& theMap, int thing)
        {
           MyMap::const_iterator it = 
              std::find_if(theMap.begin(), theMap.end(), 
                           boost::bind(&KeyEquals, _1, thing));
           return (it==theMap.end())?std::string():it->second;
        }
        

        i.e. no std::ptr_fun, no having to say that you're binding the second parameter. You can see that the order of the parameters passed to bind matches the function signature order of KeyEquals, with '_1' (a predefined placeholder) denoting that the first (and only, in this case) parameter of the bound function object is the value passed by find_if. HTH! Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

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

        thankx for that one it worked swell im just into STLs now and most of the adapter tutorials seem to have bind1st operating only on vectors so was just tweaking around with maps n function adaptors i ran into another problem....trying to use the solution above in "transform" suppose i get my data as a space seperated string list say a vector of "one 1" "two 2" "tri 3" "for 4" "fiv 5" and have to parse it into a map<string,string> i tried using the transform function like below

        typedef std::map MyMap;
        typedef std::vector MyVector;
        
        MyMap::value_type ParsetoMap(string str)
        {
        //depending on the blank position
        	string::size_type index = str.find(' ');
        //split into 2 strings
        	MyMap::value_type MyMapPair;
        	MyMapPair.first = str.substr(0, index);                       //key
        	MyMapPair.second = str.substr(index+1, (int)str.length());    //data
        	return MyMapPair;
        }
        void blah2(MyVector & theVec, MyMap const& theMap)
        {
        	transform(theVec.begin(), theVec.end(), theMap.begin(), ptr_fun(ParsetoMap));
        }
        

        but it doesnt work ???

        S 1 Reply Last reply
        0
        • F fordge

          thankx for that one it worked swell im just into STLs now and most of the adapter tutorials seem to have bind1st operating only on vectors so was just tweaking around with maps n function adaptors i ran into another problem....trying to use the solution above in "transform" suppose i get my data as a space seperated string list say a vector of "one 1" "two 2" "tri 3" "for 4" "fiv 5" and have to parse it into a map<string,string> i tried using the transform function like below

          typedef std::map MyMap;
          typedef std::vector MyVector;
          
          MyMap::value_type ParsetoMap(string str)
          {
          //depending on the blank position
          	string::size_type index = str.find(' ');
          //split into 2 strings
          	MyMap::value_type MyMapPair;
          	MyMapPair.first = str.substr(0, index);                       //key
          	MyMapPair.second = str.substr(index+1, (int)str.length());    //data
          	return MyMapPair;
          }
          void blah2(MyVector & theVec, MyMap const& theMap)
          {
          	transform(theVec.begin(), theVec.end(), theMap.begin(), ptr_fun(ParsetoMap));
          }
          

          but it doesnt work ???

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4
          transform(theVec.begin(), theVec.end(), std::inserter(theMap, theMap.begin()), &ParsetoMap);
          

          Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'

          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