473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

is it possible to bind 2 default parameters using function object(STL)?

here is what i what to do;
//pesudo code
iter = container.begin();
while (iter != container.end())
{
myclass *obj =my_cast (*iter);
obj->func(arg1, arg2);
}

i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?
thanks in advance :)

Feb 27 '06 #1
4 2647
thinktwice wrote:
here is what i what to do;
//pesudo code
iter = container.begin();
while (iter != container.end())
{
myclass *obj =my_cast (*iter);
obj->func(arg1, arg2);
}

i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?
thanks in advance :)

What you probably need is a Functor object. Something like the code below:

class FunctorExample
{
private:
int arg2;
Obj &Data;
public:
FunctorExample(Obj &Ref, int Val) : Data(Ref),arg2(val) {}
void operator() (int arg1)
{
Data.func(arg1,arg2);
}
};

{
// Some sample code
Obj ExampleObj;
FunctorExample FnctObj(ExampleObj,6);
std::for_each(container.begin(),container.end(),&F nctObj);
}

With a more accurate description/example of your requirements
this could probably be modified to suit.

JB
Feb 27 '06 #2

thinktwice wrote:
here is what i what to do;
...horrible for loop code eaten out
i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?
thanks in advance :)


Hello.
First, use Boost, Boost is your friend, Boost won't harm you :) More
specifically, I'd recommend you to use Boost.Bind for simplicity sake.
Second, you should've probably use mem_fn here as you're referring to
the obj's class namespace.
Here what kind of pseudocode you should be left over with:
std::for_each(container.begin(),container.end(),
boost::bind<ReturnType>(mem_fn(&myclass::func,arg1 ,arg2)));

Feb 27 '06 #3
thinktwice wrote:
here is what i what to do;
//pesudo code
iter = container.begin();
while (iter != container.end())
{
myclass *obj =my_cast (*iter);
obj->func(arg1, arg2);
}

i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?


It's possible if you write your own functor. The standard means only
exist for no- and one-argument member functions.

#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
using namespace std;

struct foo {
void bar(int one, double two) {
cout << this << " : " << one << " and " << two << endl;
}
};

template<class T, class A1, class A2> struct mem_fun_2_t {
void (T::*memptr)(A1, A2);
A1 arg1;
A2 arg2;
mem_fun_2_t(void (T::*mp)(A1, A2), A1 a1, A2 a2)
: memptr(mp), arg1(a1), arg2(a2) {}
void operator()(T& t) { return (t.*memptr)(arg1, arg2); }
};

template<class T, class A1, class A2>
mem_fun_2_t<T,A1,A2> mem_fun_2(void (T::*mp)(A1, A2), A1 a1, A2 a2)
{
return mem_fun_2_t<T,A1,A2>(mp, a1, a2);
}

int main()
{
vector<foo> v(10);
for_each(v.begin(), v.end(), mem_fun_2(&foo::bar, 42, 3.14159));
}

-----------------
There is probably a nicer solution in Boost, but I don't know it and
I am too lazy to look for it.

V
--
Please remove capital As from my address when replying by mail
Feb 27 '06 #4
Alex Beluga wrote:
thinktwice wrote:
here is what i what to do;
...horrible for loop code eaten out
i want to simplify those code just like this:
std::for_each(conatiner.begin(), conatiner.end(), ???)

is it possible? how to achieve?
thanks in advance :)


Hello.
First, use Boost, Boost is your friend, Boost won't harm you :) More
specifically, I'd recommend you to use Boost.Bind for simplicity sake.
Second, you should've probably use mem_fn here as you're referring to
the obj's class namespace.
Here what kind of pseudocode you should be left over with:
std::for_each(container.begin(),container.end(),
boost::bind<ReturnType>(mem_fn(&myclass::func,arg1 ,arg2)));


Not quite.

boost::bind( &myclass::func, _1, arg1, arg2 ) );

There are very few cases where ReturnType needs to be explicitly specified,
mem_fn is not necessary, and you forgot that member functions take an
implicit this pointer.

Jeff Flinn
Feb 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: kushalsoftpro | last post by:
Hi I am using STL map in VC++6.0 application. type of project is MFC DLL. My code looks like:-- typedef std::map <unsigned long,LPVOID, BOOL> MyMap; In class definition file i am using this...
3
by: Mark P | last post by:
Hi, I'm looking for some info on the default STL allocator, std::alloc. In particular, I'm wondering if it is optimized to handle many allocations of small objects. I'm thinking along the...
2
by: Steve Terepin | last post by:
Do I need to include some extra libraries when I use the STL <string> classes ? I've created a new Class Library (.NET) project, added a #include <string> directive to the main .cpp file, and...
5
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a...
1
by: recover | last post by:
#include <xxx> int main() { const wchar* pwcHello=L"hello"; char* pcHello; xxxxxx //do something using stl cout<<pcHello<<endl; } =============out===========
8
by: writeanand | last post by:
How can I count the frequency of words in a ASCII File using STL? a) I dont know what words will be found in the file b) The max number of occurrences is 10,000 per word (in case that matters) ...
4
by: writeanand | last post by:
How can I count the frequency of words in a ASCII File using STL? I dont know what words will be found in the file ahead of time. I dont want to use any classes, just a simple program wd do. ...
5
by: Chelong | last post by:
hey,the follow is the text file content ========================================apple====pear== one Lily 7 0 0 7 7 two Lily 20 20 6.6666 20 8 one Lily 0 10 2.85 4 0 two Lily 22 22 7.33326 2 5 ...
1
by: parvtb | last post by:
I know STL vector works. But in case STL is not available, what can one do to allocate large memory size in c++ through operator "new"? For instance, I am writing a sort algorithm, and here's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.