Re: How do you compare char strings in an elegant way?
{ Edits: quoted clc++m banner removed. Please don't quote the banner.
-mod }
<daniel_t-35C1DA.10085607102007@earthlink.vsrv-sjc.supernews.net>
References: <1191727909.081053.284760@y42g2000hsy.googlegroups.com>
X-Clcppm-Sequence: 8519
X-Original-Date: Sun, 7 Oct 2007 10:36:42 CST
X-Submission-Address: c++-submit@netlab.cs.rpi.edu
In article <1191727909.081053.284760@y42g2000hsy.googlegroups.com>,
isometric_god@gmx.de wrote:
Hello,
I have this very simple task, but I cant get it to work:
there is this
std::map<const char* str, int> my_map;
and I would like to compare the requested strings using strcmp.
It works like this :
bool comp(const char* s1, const char* s2)
{
return _stricmp(s1, s2) < 0;
}
my_map(std::ptr_fun(comp));
A neat solution would be to model the function with STL functor
objects in a combination similiar to
bind2nd(less(strcmp), 0);
which is total garbage, but maybe you get the idea.
thank you
typedef compose_f_gxy_t<binder2nd<less<int> >,
pointer_to_binary_function<const char*, const char*, int> > ltstr;
map<const char*, int, ltstr > my_map(
compose_f_gxy( bind2nd( less<int>(), 0 ), ptr_fun( &strcmp ) ) );
To use the above you need a compose functor:
// (C) Copyright Nicolai M. Josuttis 1999.
// Permission to copy, use, modify, sell and distribute this software
// is granted provided this copyright notice appears in all copies.
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
/* class for the compose_f_gxy adapter */
template <class OP1, class OP2>
class compose_f_gxy_t :
public std::binary_function<typename OP2::first_argument_type,
typename OP2::second_argument_type, typename OP1::result_type>
{
private:
OP1 op1; // process: op1(op2(x,y))
OP2 op2;
public:
// constructor
compose_f_gxy_t ( const OP1& o1, const OP2& o2 ) :
op1( o1 ), op2( o2 ) {
}
// function call
typename OP1::result_type operator() (
const typename OP2::first_argument_type& x,
const typename OP2::second_argument_type& y ) const {
return op1( op2( x, y ) );
}
};
/* convenience function for the compose_f_gxy adapter */
template <class OP1, class OP2>
inline compose_f_gxy_t<OP1, OP2> compose_f_gxy (
const OP1& o1, const OP2& o2 ) {
return compose_f_gxy_t<OP1, OP2>( o1, o2 );
}
----------------------------------------------------------------------
Now the question is, can this be done with the boost lambda library? It
would probably look much cleaner.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]