c++ - stl functor with more than 2 arguments -
i working stl don't have c++0x , can't use boost, wonder if there anyway bind 2 or more arguments functor when use std::generate? like
#include <iostream> #include <vector> #include <algorithm> #include <functional> using namespace std; float f(int x, float y, float z) {return x*(y+z);} int main(void) { std:vector<int> v(100); float x=1.2, y=-3.3; generate(v.begin(), v.end(), bind3argu(f, _, x, y)); // this: '_' vector // suggested, try generate(v.begin(), v.end(), std::bind(f, x, y)); return 0; }
i try use std::bind doesn't compile g++ 4.4.6. btw, std::bind being supported in c++0x and/or c++11 only?
if intend use functors, try using std::transform
.
float foo(float z) { float x=1.2, y=-3.3; return x*(y+z); } std::transform(v.begin(), v.end(), v.begin(), foo);
the real problem how pass vector value well. had if not case, std::bind
useful.
std::generate(v.begin(), v.end(), std::bind(f, x, y /*any number of varaibales*/));
Comments
Post a Comment