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???
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'
-
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'
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 amap<string,string>
i tried using the transform function like belowtypedef 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 ???
-
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 amap<string,string>
i tried using the transform function like belowtypedef 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 ???
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'