We know that alomost every algorithm function, such as for_each,
find_if, use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See example:
class TestPrint
{
public:
TestPrint( int i ) { start_num = i; }
void print( int i )
{
cout << endl<< "| " << i + start_num <<
" |" << endl;
}
private:
int start_num;
};
int main()
{
TestPrint* pTest = new TestPrint( 8 );
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
delete pTest;
return 0;
}
/*** Use the pTest's "print" ***/ is where I want to write code . 9 2086
On Jul 12, 4:54*pm, "want.to.be.professer" <guolihui...@gmail.com>
wrote:
We know that alomost every algorithm function, such as for_each,
find_if, *use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See example:
class TestPrint
{
public:
* * * * TestPrint( int i ) { start_num = i; }
* * * * void print( int i )
* * * * {
* * * * * * * * cout << endl<< "| * * * *" << i +start_num <<
" * * * * * * * *|" << endl;
* * * * }
private:
* * * * int start_num;
};
int main()
{
* * * * TestPrint* pTest = new TestPrint( 8 );
* * * * int *a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
* * * * for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
* * * * delete pTest;
* * * * return 0;
}
/*** Use the pTest's "print" ***/ is where I want to write code .
sorry
for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
should be
for_each( a, a + 10, /*** Use the pTest's "print" ***/ );
On Jul 12, 1:59*pm, "want.to.be.professer" <guolihui...@gmail.com>
wrote:
On Jul 12, 4:54*pm, "want.to.be.professer" <guolihui...@gmail.com>
wrote:
We know that alomost every algorithm function, such as for_each,
find_if, *use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See example:
class TestPrint
{
public:
* * * * TestPrint( int i ) { start_num = i; }
* * * * void print( int i )
* * * * {
* * * * * * * * cout << endl<< "| * * * *" << i+ start_num <<
" * * * * * * * *|" << endl;
* * * * }
private:
* * * * int start_num;
};
int main()
{
* * * * TestPrint* pTest = new TestPrint( 8 );
* * * * int *a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
* * * * for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
* * * * delete pTest;
* * * * return 0;
}
/*** Use the pTest's "print" ***/ is where I want to write code .
sorry
* * * * for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
should be
* * * * for_each( a, a + 10, /*** Use the pTest's "print" ***/ );
You can use binders like boost::bind, or standard binders like bind1st
(here) / bind2nd along with std::mem_fun_ref.
for_each( a, a + 9, bind(&TestPrint::print, pTest, _1));
Make the print member a const member function, else std:: binders
won't probably work.
On Jul 12, 5:26*pm, Abhishek Padmanabh <abhishek.padman...@gmail.com>
wrote:
On Jul 12, 1:59*pm, "want.to.be.professer" <guolihui...@gmail.com>
wrote:
On Jul 12, 4:54*pm, "want.to.be.professer" <guolihui...@gmail.com>
wrote:
We know that alomost every algorithm function, such as for_each,
find_if, *use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See example:
class TestPrint
{
public:
* * * * TestPrint( int i ) { start_num = i; }
* * * * void print( int i )
* * * * {
* * * * * * * * cout << endl<< "| * * * *" <<i + start_num <<
" * * * * * * * *|" << endl;
* * * * }
private:
* * * * int start_num;
};
int main()
{
* * * * TestPrint* pTest = new TestPrint( 8 );
* * * * int *a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
* * * * for_each( a, a + 9, /*** Use the pTest's "print" ***/);
* * * * delete pTest;
* * * * return 0;
}
/*** Use the pTest's "print" ***/ is where I want to write code .
sorry
* * * * for_each( a, a + 9, /*** Use the pTest's "print" ***/);
should be
* * * * for_each( a, a + 10, /*** Use the pTest's "print" ***/ );
You can use binders like boost::bind, or standard binders like bind1st
(here) / bind2nd along with std::mem_fun_ref.
* * * * * * * * for_each( a, a + 9, bind(&TestPrint::print, pTest, _1));
Make the *print member a const member function, else std:: binders
won't probably work.
please tell me how to use standard binders like bind1st
(here) / bind2nd along with std::mem_fun_ref.
Thank you!
On Jul 12, 4:54*am, "want.to.be.professer" <guolihui...@gmail.com>
wrote:
We know that alomost every algorithm function, such as for_each,
find_if, *use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See example:
class TestPrint
{
public:
* * * * TestPrint( int i ) { start_num = i; }
* * * * void print( int i )
* * * * {
* * * * * * * * cout << endl<< "| * * * *" << i +start_num <<
" * * * * * * * *|" << endl;
* * * * }
private:
* * * * int start_num;
};
int main()
{
* * * * TestPrint* pTest = new TestPrint( 8 );
* * * * int *a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
* * * * for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
* * * * delete pTest;
* * * * return 0;
}
/*** Use the pTest's "print" ***/ is where I want to write code .
First the obvious:
struct foo
{
TestPrint* testPrint;
foo( TestPrint* tp ): testPrint( tp ) { }
void operator()( int i ) {
testPrint->print( i );
}
};
for_each( a, a + 9, foo( pTest ) );
the above structure can be built with the standard functional objects
as well:
for_each( a, a + 9, bind1st( mem_fun( &TestPrint::print ), pTest ) );
mem_fun wraps TestPrint::print into a function object that takes two
parameters (TestPrint* and int), and bind1st wraps the mem_fun into a
function object that always passes pTest as the first parameter.
On Jul 12, 10:24*pm, "Daniel T." <danie...@earthlink.netwrote:
On Jul 12, 4:54*am, "want.to.be.professer" <guolihui...@gmail.com>
wrote:
We know that alomost every algorithm function, such as for_each,
find_if, *use funcional as well as function pointer. But when I want
to use another class's member function, how could I do?
See example:
class TestPrint
{
public:
* * * * TestPrint( int i ) { start_num = i; }
* * * * void print( int i )
* * * * {
* * * * * * * * cout << endl<< "| * * * *" << i+ start_num <<
" * * * * * * * *|" << endl;
* * * * }
private:
* * * * int start_num;
};
int main()
{
* * * * TestPrint* pTest = new TestPrint( 8 );
* * * * int *a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
* * * * for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
* * * * delete pTest;
* * * * return 0;
}
/*** Use the pTest's "print" ***/ is where I want to write code .
First the obvious:
struct foo
{
* * * * TestPrint* testPrint;
* * * * foo( TestPrint* tp ): testPrint( tp ) { }
* * * * void operator()( int i ) {
* * * * * * * * testPrint->print( i );
* * * * }
};
for_each( a, a + 9, foo( pTest ) );
the above structure can be built with the standard functional objects
as well:
for_each( a, a + 9, bind1st( mem_fun( &TestPrint::print ), pTest ) );
mem_fun wraps TestPrint::print into a function object that takes two
parameters (TestPrint* and int), and bind1st wraps the mem_fun into a
function object that always passes pTest as the first parameter.
en, Thank you
for_each( a, a + 9, bind1st( mem_fun( &TestPrint::print ), pTest ) );
this is what I want,
I'd better review the STL.
In article <e66af743-5009-4e68-bfe6-5734bd324ab4
@k13g2000hse.googlegroups.com>, gu*********@gmail.com says...
[ ... ]
class TestPrint
{
public:
TestPrint( int i ) { start_num = i; }
void print( int i )
{
cout << endl<< "| " << i + start_num <<
" |" << endl;
}
private:
int start_num;
};
int main()
{
TestPrint* pTest = new TestPrint( 8 );
Why are you allocating this dynamically? You're doing dynamic
allocation, but using it to imitate automatic allocation (i.e.
allocating it as you enter a scope and deleting it as you leave that
scope). With that given, automatic allocation would be far more suitable
under the circumstances.
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for_each( a, a + 9, /*** Use the pTest's "print" ***/ );
The direct, obvious answers to your question would involve things like
mem_fun or mem_fun_ref. Personally, however, I've become convinced that
while these were well intended, using them is a mistake far more often
than not. Code using them is rarely readable, and (more importantly)
over time I've become convinced that they're _usually_ a sign of poor
design anyway.
It's less obvious, but when you get down to it, TestPrint is really a
functor. All you ever do with it is create it, and then invoke one
member function -- and that member function is basically a repetition of
the verb in the name of the class. The 'print' doesn't add any useful
information -- TestPrint() would mean as much as TestPrint::print().
General observation: when a class name is primarily a verb instead of a
noun, there's a pretty good chance that class should be a functor.
What I think you really should do is change your code to more accurately
reflect the nature of TestPrint, making it a real functor.
class TestPrint {
int start_num;
public:
TestPrint(int i) : start_num(i) {}
void operator()(int i) {
cout << endl<< "| "
<< i + start_num
<< " |" << endl;
}
};
Having done that, invocation becomes trivial:
int main() {
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for_each(a, a+9, TestPrint(8));
return 0;
}
Contrary to what you said elsthread, in this case it should be a+9, NOT
a+10. IMO, it's worthwhile to write code to handle this for you though:
template <class T, size_t N>
T *end(T (&a)[N]) {
return a+N;
}
Then you can use:
for_each(a, end(a), TestPrint(8));
Though it's not really related to your original question, there's also
the minor detail of the formatting you used for the output. At a guess,
this is probably closer to what you really wanted:
cout << "|" << setw(10) << i+start_num << setw(17) << "|" << endl;
--
Later,
Jerry.
The universe is a figment of its own imagination.
On Jul 12, 11:58*am, Jerry Coffin <jcof...@taeus.comwrote:
<snip>
Contrary to what you said elsthread, in this case it should be a+9, NOT
a+10. IMO, it's worthwhile to write code to handle this for you though:
template <class T, size_t N>
T *end(T (&a)[N]) {
* * * * return a+N;
All great points here; I very much agree. Except "a+10" _is_ correct.
Although we won't hold you responsible, since your helper function
would return the correct amount in this case.
Joe Cook
On Jul 12, 11:58*pm, Jerry Coffin <jcof...@taeus.comwrote:
In article <e66af743-5009-4e68-bfe6-5734bd324ab4
@k13g2000hse.googlegroups.com>, guolihui...@gmail.com says...
Thanks for your explaination.But I cann't get the follow advise :
Contrary to what you said elsthread, in this case it should be a+9, NOT
a+10. IMO,
Let's look at the source code of " for_each ":
// for_each. Apply a function to every element of a range.
template <class _InputIter, class _Function>
_Function for_each(_InputIter __first, _InputIter __last,
_Function __f) {
__STL_REQUIRES(_InputIter, _InputIterator);
for ( ; __first != __last; ++__first)
__f(*__first);
return __f;
}
Note the "__first != __last" .When used a+9, at the end, __first ==
&a[9], but a[9] is never printed.
So I thind "a+10" is correct.
In article <d5fd36ec-ce61-49d4-b1f1- 05**********@d1g2000hsg.googlegroups.com>, gu*********@gmail.com says...
On Jul 12, 11:58Â*pm, Jerry Coffin <jcof...@taeus.comwrote:
In article <e66af743-5009-4e68-bfe6-5734bd324ab4
@k13g2000hse.googlegroups.com>, guolihui...@gmail.com says...
Thanks for your explaination.But I cann't get the follow advise :
Contrary to what you said elsthread, in this case it should be a+9, NOT
a+10. IMO,
As well you shouldn't -- I just plain screwed up.
--
Later,
Jerry.
The universe is a figment of its own imagination. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Wahoo |
last post by:
Dear All,
What is the name of sorting algorithm used in list (STL) class? and how it
work?
Thanks!!!
Best Regards,
Wahoo
|
by: Tony Johansson |
last post by:
Hello!
I have some problem with STL function remove
I have two classes called Handle which is a template class and Integer which
is not a template class. The Integer class is just a wrapper...
|
by: sd2004 |
last post by:
could someone please show/help me to copy all element from "class dog"
to "class new_dog" ?
Note: "class new_dog" has new element "age" which should have value
"my_age"...
|
by: Alden Pierre |
last post by:
Hello,
I'm trying to create my own user define container, but I'm having a
little hard time figuring out why is my class considered undefined by my
compiler. Here is the following code.
//...
|
by: Jef Driesen |
last post by:
Suppose I have a datastructure (actually it's a graph) with one template
parameter (the property P for each edge and vertex):
struct graph<P>;
struct vertex<P>;
struct edge<P>;
I also have...
|
by: Belebele |
last post by:
I have an Element class which is abstract and I would like to have an
object of the Iterator class to iterate over a range of elements.
I would like to use std::for_each to instrument the...
|
by: mweltin |
last post by:
I have a derived class that extends a base class by adding a float
member. The strange part is when I use the STL sort algorithm on a
vector of derived classes, only hte float member is sorted....
|
by: Kira Yamato |
last post by:
I've posted this in another thread, but I suppose I should've started a
new thread for it instead.
I cannot get the following short program to compile under g++:
#include <iostream>
#include...
|
by: digz |
last post by:
Hi ,
I am trying to write a class that encapsulates a Union within it , I
intend the code to be exception safe.
can someone please review it and tell me if I have done the right
thing , are...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
| |