473,765 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(c onatiner.begin( ), conatiner.end() , ???)

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

Feb 27 '06 #1
4 2662
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(c onatiner.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(Example Obj,6);
std::for_each(c ontainer.begin( ),container.end (),&FnctObj);
}

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(c onatiner.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(c ontainer.begin( ),container.end (),
boost::bind<Ret urnType>(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(c onatiner.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(voi d (T::*mp)(A1, A2), A1 a1, A2 a2)
: memptr(mp), arg1(a1), arg2(a2) {}
void operator()(T& t) { return (t.*memptr)(arg 1, arg2); }
};

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

int main()
{
vector<foo> v(10);
for_each(v.begi n(), 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(c onatiner.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(c ontainer.begin( ),container.end (),
boost::bind<Ret urnType>(mem_fn (&myclass::func ,arg1,arg2)));


Not quite.

boost::bind( &myclass::fu nc, _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
2177
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 map as:-- MyMap m_mapPHLRecorder ;
3
3256
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 lines of a pool-based approach, for example. Any references would also be appreciated. Thanks,
2
1383
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 created an instance of a std::string ; this compiles fine, but the linker reports two unresolved symbols - '_CxxThrowException' and 'delete'. Any ideas ?
5
4322
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 vector of boost::shared_ptr's, some of which may be NULL. I'd like to find out if there are any non-NULL elements in the vector. Here's what I have at the moment.
1
3363
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
4330
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) Can anyone give me an example program and compare/contrast the use of various STL containers for this problem?
4
2856
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. Thanks!!!
5
1885
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 two jenny 6 0 0 6 6 two jenny 12 0 0 12 12 three jenny 36 36 10.26 36 36
1
2146
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 part of the code: const int range = 1000000; int *array = new int(range); if (array == 0) cout<<"null ptr returned"<<endl;
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9835
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.