-
October 26th, 2009, 05:06 AM
#1
[RESOLVED] mem_fun & templates
This is the first time I've used std::mem_fun and I'm at a loss to figure out what I have to do to get this to compile.
Code:
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
template <typename T>
class Test
{
public:
void Function1(T &t)
{
}
void Function2()
{
for_each(data.begin(), data.end(), mem_fun(&Test<T>::Function1)); // Error here.
}
private:
vector<T> data;
};
int main()
{
Test<int> test;
test.Function2();
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
October 26th, 2009, 05:11 AM
#2
Re: mem_fun & templates
The fact it is a template seems to be irrelevant.
Code:
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
class Test
{
public:
void Function1(int t)
{
}
void Function2()
{
for_each(data.begin(), data.end(), mem_fun(&Test::Function1));
}
private:
vector<int> data;
};
int main()
{
Test test;
test.Function2();
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
October 26th, 2009, 05:17 AM
#3
Re: mem_fun & templates
Found the answer!
Code:
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
class Test
{
public:
void Function1(int t)
{
}
void Function2()
{
for_each(data.begin(), data.end(), bind1st(mem_fun(&Test::Function1), this));
}
private:
vector<int> data;
};
int main()
{
Test test;
test.Function2();
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
October 28th, 2009, 07:43 AM
#4
Re: [RESOLVED] mem_fun & templates
Can you teach me how bind1st and bind2nd works because i read a lot of doc but never understand ?
Thanks.
Thanks for your help.
-
October 29th, 2009, 04:16 AM
#5
Re: [RESOLVED] mem_fun & templates
bind1st & bind2nd allow you to use a binary function (two parameters) where a unary function (one parameter) is expected.
Take std::greater for example.
This is a binary function, but it's often used where a unary function is expected, for example std::count_if.
If you wanted to count how many items in a container were greater than 10 then you would use the following.
Code:
#include <functional>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
vector<int> data;
for (int i = 0; i < 100; ++i)
{
data.push_back(rand());
}
vector<int>::size_type count = count_if(data.begin(), data.end(), bind2nd(greater<int>(), 10));
}
bind2ndt makes the second argument to greater always equal to 10.
The first argument is the value of the element in the vector.
It's functionally equivalent to
Code:
#include <functional>
#include <vector>
#include <algorithm>
#include <cstdlib>
using namespace std;
int main()
{
vector<int> data;
for (int i = 0; i < 100; ++i)
{
data.push_back(rand());
}
greater<int> test;
vector<int>::size_type count = 0;
for (vector<int>::size_type i = 0; i < data.size(); ++i)
{
if (test(data[i], 10))
{
++count;
}
}
}
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|