473,386 Members | 1,798 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,386 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 3161
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öm

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 seem to be working: <xsl:if...
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 trying to implement a template in an XSL stylesheet...
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>& x) {
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 array to another? This is my current code //...
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 throughout the tree. <text> .... <page>1</page>...
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 (pulled from sql server table with a data type of...
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 contain a single '\'? Is it better to use...
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 figure out a mechanism within XSLT to compare the...
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. .... }; Here I am not passing compare function,...
0
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,...
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: 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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...
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...

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.