Am I missing something about STL predicates?

Am I missing something about STL predicates?

Post by Arne Schwee » Wed, 14 Jul 1999 04:00:00



Hi,

i recently tried to use some STL algorithmn upon the contents of a
container. The algortihm takes some predicate as argument that is true
only for some of the containers elements.

It's no problem to write a predicate derived from unary_function to
achieve this, but I was curious about doing this by combining predefined
predicates from <functional>:

What i had to do was: call a elements member function and compare the
result with some value.

The function mem_fun gives me the first problem: How can I call a const
member functions without a ugly cast? All the mem_fun types are declared
calling non const member functions!

Second, How can I combine two predicates to one? There are binder
predicates, that bind values to arguments, but I see no possibilty to
combine my mem_fun_t (or _ref) with some eqal_to predicate.

Do I have to code this for myself or am I missing something?

regards,

        </Arne>


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Am I missing something about STL predicates?

Post by Siemel Nar » Thu, 15 Jul 1999 04:00:00



Quote:>The function mem_fun gives me the first problem: How can I call a const
>member functions without a ugly cast? All the mem_fun types are declared
>calling non const member functions!

No.  There are four overloaded mem_fun functions.

The first two mem_fun's take non-const and const member functions of zero
arguments, and return mem_fun_t and const_mem_fun_t objects respectively.
The operator() of these two objects take one argument, namely a pointer
to a non-const or const object.

The second two mem_fun's take non-const and const member functions of one
argument, and return mem_fun1_t and const_mem1_fun_t objects respectively.
The operator() of these two objects take two arguments, namely a pointer
to a non-const or const object, as well as the argument of the original
member function.

Then there are four overloaded mem_fun_ref functions.

There are no functions mem_fun1 and mem_fun1_ref in the standard,
although some implementations still carry them, as a remembrance of
times when dinosours ruled the earth :).

Quote:>Second, How can I combine two predicates to one? There are binder
>predicates, that bind values to arguments, but I see no possibilty to
>combine my mem_fun_t (or _ref) with some eqal_to predicate.

>Do I have to code this for myself or am I missing something?

Here is one example:

First, here is a class.  Pay attention to the public get functions
name() and size().
      class Type
      {
         public:
            Type(char const * name, size_t size)
               : d_name(name), d_size(size) { }
            char const * name() const { return d_name; }
            size_t size() const { return d_size; }
         private:
            char const *const d_name;
            const size_t d_size;
      };

Now here is the body of some function
   typedef std::deque<Type>::iterator Iter;
   using std::compose1;
   using std::bind2nd;
   using std::mem_fun_ref;
   typedef std::equal_to<size_t> equal;
   const Iter pos=std::find_if(
      d_types.begin(), d_types.end(),
      compose1(bind2nd(equal(),size),mem_fun_ref(&Type::size))
   );
   if (pos==d_types.end()) { ... } // not found

The find_if statement finds the first element in the array d_types
(an array of Type objects) that which has its size field, given
by Type::size(), equal to the local variable 'size'.

Whether you want to write code like this is another matter.

--
----------------------------------

----------------------------------


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

Am I missing something about STL predicates?

Post by Martin Fabia » Tue, 27 Jul 1999 04:00:00




> >The function mem_fun gives me the first problem: How can I call a const
> >member functions without a ugly cast? All the mem_fun types are declared
> >calling non const member functions!

> No.  There are four overloaded mem_fun functions.

But not in Dinkumware's MSVC++6 implementation of the STL. The consts
are not there.
--
Martin Fabian                         http://www.s2.chalmers.se/~fabian/
                                                                      --
"Cheer up. It may never happen"                          (Edina Monsoon)

        /* Remove NOSPAM from reply-to address to mail me */


      [ about comp.lang.c++.moderated. First time posters: do this! ]

 
 
 

1. ComboBox - Am I MISSING something?

Help!

        Ok, I've used combo-boxes before...even successfully.  But right
now I'm trying to use a DROPDOWN type, and get notified when the user
picks something from the connected list box.

        The CBN_CLOSEUP notification doesn't work, because if you do a
GetWindowText right after getting notified, you get the ~previous~
contents (before the selection from the list box).  CBN_SELCHANGE
and CBN_EDITCHANGE don't work either.

        Note: If I ~type~ in my selection, I get notified correctly.

        Any ideas?

Greg

2. HSC70 Software

3. Do the books oversimplify, or am I missing something?

4. Q: looking for font

5. Undeclared identifier: _beginthread. Am I missing something?

6. SuperCalc 5.5 to Quattro Pro 6.01

7. Am i missing something with this simple struct syntax?

8. MAC & MIDI

9. Am I missing something about XP and Documentation??

10. Logic Error am I missing something simple here ?

11. Am I missing something in the DHTML Editor?

12. Am I missing something "new"?