472,342 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

compare function template

I need to implement a function with a argument that is a compare
function.
This compare function must be several for every necessity.
For example , I would like a compare function to analyze element of
list that are even or a compare function to analyze odd element.
example:

void f (compare )
{

list<intl;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( compare(it) )
{
....
}

}

}
I would like specify in my call, what type of compare function the
function f must use.
Exist a template solution for that?

Mar 7 '07 #1
5 3060
On 7 Mar, 12:26, "antani" <antani8...@yahoo.itwrote:
I need to implement a function with a argument that is a compare
function.
This compare function must be several for every necessity.
For example , I would like a compare function to analyze element of
list that are even or a compare function to analyze odd element.

example:

void f (compare )
{

list<intl;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( compare(it) )
{
....
}

}

}

I would like specify in my call, what type of compare function the
function f must use.
Exist a template solution for that?
I'm not quite sure I understand what you want but you probably want to
use templates here, something like:

template<typename CMP>
void f(CMP comp)
{
list<intl;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( comp(it) )
{
....
}
}
}

This way you can use either a functor or a normal function when
calling f().

--
Erik Wikstr鰉

Mar 7 '07 #2
On Mar 7, 1:26 pm, "antani" <antani8...@yahoo.itwrote:
I need to implement a function with a argument that is a
compare function.
This compare function must be several for every
necessity. For example , I would like a compare function
to analyze element of list that are even or a compare
function to analyze odd element.
I almost choked on my coffee when my compiler didn't choke
on the following:

#include <iostream>
#include <ostream>
#include <list>

template < typename T >
bool fredle ( T x ) { return ( * x ) % 2 ; }

template < typename T >
bool bargle ( T x ) { return ! ( ( * x ) % 2 ) ; }

template < typename T >
bool quugle ( T x ) { return ! ( ( * x ) % 3 ) ; }

template < typename T >
bool xyzzle ( T x ) { return quugle ( -- x ) ; }

template < typename T >
void f
(
const T & l ,
bool ( * c ) ( typename T :: const_iterator )
)
{
for
(
typename T :: const_iterator i = l . begin ( ) ;
i != l . end ( ) ; ++ i
)
if ( ( * c ) ( i ) ) std :: cout << ( * i ) << " " ;
std :: cout << std :: endl ;
}

int main ( )
{
std :: list < int l ;
for ( int i = 0 ; i != 10 ; ++ i )
l . push_back ( i + 1 ) ;
f ( l , & fredle ) ;
f ( l , & bargle ) ;
f ( l , & quugle ) ;
f ( l , & xyzzle ) ;
}

Looks like a disaster waiting to happen to me.

--
Pavel Lepin

Mar 7 '07 #3
template<typename CMP>
void f(CMP comp)
{
list<intl;
list<int>::iterator it;

for (it=l.begin();l!=l.end();++it)
{
if ( comp(it) )
{
....
}
}
}

This way you can use either a functor or a normal function when
calling f().
Can you write a example how declare a CMP comp?

Mar 7 '07 #4
On 3月7日, 下午9时46分, "antani" <antani8...@yahoo.itwrote:
template<typename CMP>
void f(CMP comp)
{
list<intl;
list<int>::iterator it;
for (it=l.begin();l!=l.end();++it)
{
if ( comp(it) )
{
....
}
}
}
This way you can use either a functor or a normal function when
calling f().

Can you write a example how declare a CMP comp?- 隐藏被引用文字 -
for example:

#include <iostream>

// the normal function
void foo()
{
std::cout << "foo" << std::endl;
}

// the function object
class bar
{
void operator ()(){
std::cout << "bar" << std::endl;
}
}

template <typename CMP>
void foobar(CMP cmp)
{
cmp();
}

int main()
{
foobar(foo);
foobar(bar());

return 0;
}
- 显示引用的文字 -

Mar 7 '07 #5
On 3月7日, 下午10时19分, "Wayne Shu" <Wayne...@gmail.comwrote:
On 3月7日, 下午9时46分, "antani" <antani8...@yahoo.itwrote:
template<typename CMP>
void f(CMP comp)
{
list<intl;
list<int>::iterator it;
for (it=l.begin();l!=l.end();++it)
{
if ( comp(it) )
{
....
}
}
}
This way you can use either a functor or a normal function when
calling f().
Can you write a example how declare a CMP comp?- 隐藏被引用文字 -

for example:

#include <iostream>

// the normal function
void foo()
{
std::cout << "foo" << std::endl;

}

// the function object
class bar
{
sorry I have forgotten to add the access level.
public:
void operator ()(){
std::cout << "bar" << std::endl;
}

}

template <typename CMP>
void foobar(CMP cmp)
{
cmp();

}

int main()
{
foobar(foo);
foobar(bar());

return 0;

}
- 显示引用的文字 -- 隐藏被引用文字 -

- 显示引用的文字 -- 隐藏被引用文字 -

- 显示引用的文字 -

Mar 7 '07 #6

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

Similar topics

5
by: Nasos Makriyiannis | last post by:
Hi, I'm new to XSL and I was wondering if there is a string-compare function available. I am using the following IF statement but it does not...
1
by: Frank | last post by:
Hi, is there a way to use XSL to compare two XML files to verify if a "record" in an XML file has changed of parent in another XML file ? I am...
7
by: Sean J. Fraley | last post by:
This code illustrates what I'm confused about: template<typename T> class foo { public: template<typename U> void fooFunction(const foo<U>&...
13
by: MrCoder | last post by:
Hey guys, my first post on here so I'll just say "Hello everbody!" Ok heres my question for you lot. Is there a faster way to compare 1 byte...
2
by: Cali | last post by:
Please bear with me, I have been reading about XSL for a couple hours. I have an XML document that contains multiple <page> tags interspersed...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid...
10
by: lovecreatesbea... | last post by:
Is it correct and safe to compare a string object with "", a pair of quotation marks quoted empty string?If the string object: s = ""; does s...
3
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to...
5
by: S S | last post by:
Hi I have a requirement where I am declaring a map within a class. class abc { map <void*, void*mMap; // I do not pass compare struct here....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.