|
-
October 31st, 2002, 01:16 PM
#1
std::for_each question.
Hello to all,
Here we go :
Suppose I have a function that takes an int as the only parameter and returns nothing. Let's call it Func1(int).
Suppose, next, that I have a std::vector<int> named coll.
Is there any way to call Func1 for each element in the vector ???
Here's what I tried :
Code:
std::for_each(coll.begin(), coll.end(), std::mem_fun(&Func1));
This doesn't work. I've looked at the bind2st algo in the std but i'm not sure how it works. Can someone help me on this please.
Thanks in advance for any help.
Luc.
-
October 31st, 2002, 01:40 PM
#2
Should this work ?
Code:
std::for_each(coll.begin(), coll.end(),Func1);
Usually mem_fun is used if you are sorting a
vector of objects, and you want to call a
member function of that object.
-
October 31st, 2002, 01:46 PM
#3
Might I add that Func1 is a method of a class.
Like this :
Code:
ActualRealPos::Func1(int)
{
//does something.
}
ActualRealPos::Init()
{
std::vector<int> coll;
std::for_each(coll.begin(), coll.end(), std::mem_fun(&ActualRealPos::Func1));
}
-
October 31st, 2002, 02:15 PM
#4
Do you want to modify each element as it is passed into Func1()? for_each() is non-mutatable. One solution is transform().
Kuphryn
-
October 31st, 2002, 02:24 PM
#5
Nope, I only want to call Func1 for each value that are in the vector.
Thanks,
Luc.
-
October 31st, 2002, 03:29 PM
#6
Here is an example of an function object adaptor.
Code:
template <typename T>
class FunctionObjectAdaptor : public std::unary_function<T, void>
{
public:
void operator()(const T lp) const
{
...
}
};
Kuphryn
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
|