Class pointer vector and STL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex

    Class pointer vector and STL

    Hello. I would like a simpler way to do something like this:

    class C {
    ....
    bool operator<(const C&) const;
    };

    int main()
    {
    std::vector<C*> v;
    ...
    C* x = max_element(v.b egin(),v.end()) ;
    }

    without having to write this:

    bool operator<(const C* a, const C* b)
    { return *a<*b; }

  • Ben Pope

    #2
    Re: Class pointer vector and STL

    Alex wrote:[color=blue]
    > Hello. I would like a simpler way to do something like this:
    >
    > class C {
    > ...
    > bool operator<(const C&) const;
    > };
    >
    > int main()
    > {
    > std::vector<C*> v;
    > ...
    > C* x = max_element(v.b egin(),v.end()) ;
    > }
    >
    > without having to write this:
    >
    > bool operator<(const C* a, const C* b)
    > { return *a<*b; }[/color]

    You want a function call that is simpler to write than a (simple)
    function call?

    I can only suggest getting somebody else to do it.

    Ben Pope
    --
    I'm not just a number. To many, I'm known as a string...

    Comment

    • Dietmar Kuehl

      #3
      Re: Class pointer vector and STL

      Alex wrote:
      [color=blue]
      > Hello. I would like a simpler way to do something like this:[/color]

      What exactly do you mean by "simpler"? Less repetitive lines of
      code?
      [color=blue]
      > std::vector<C*> v;
      > ...
      > C* x = max_element(v.b egin(),v.end()) ;[/color]

      This does not fit the signature of 'std::max_eleme nt()' at all:
      the function returns an iterator, not the maximum value:

      std::vector<C*> ::iterator it
      = std::max_elemen t(v.begin(), v.end(), ...);
      [color=blue]
      > without having to write this:
      >
      > bool operator<(const C* a, const C* b)
      > { return *a<*b; }[/color]

      First of all, you cannot use this operator at all: you are not allowed
      to overload operators involving only built-in types and all pointer
      types are built-in.

      It is fairly easy to create a "dereferenc e" comparator which would
      to the right thing:

      template <typename T>
      struct ptr_less {
      bool operator()(T* p1, T* p2) const { return *p1 < *p2; }
      };

      Using this class you could use 'ptr_less<C>()' instead of the "..."
      in the 'max_element()' call (I think; I haven't tested the code and
      potentially you may need to make the predicate class adaptable).
      --
      <mailto:dietmar _kuehl@yahoo.co m> <http://www.dietmar-kuehl.de/>
      <http://www.eai-systems.com> - Efficient Artificial Intelligence

      Comment

      • Alex

        #4
        Re: Class pointer vector and STL

        I want to know if there is some function in the STL to apply a class
        method to an array of pointers to that class.

        Something similar to mem_fun, but for class pointers.

        Comment

        • Alex

          #5
          Re: Class pointer vector and STL

          mem_fun_t is exactly what I was looking for. Thanks anyway.

          Comment

          • Ben Pope

            #6
            Re: Class pointer vector and STL

            Alex wrote:[color=blue]
            > I want to know if there is some function in the STL to apply a class
            > method to an array of pointers to that class.[/color]

            OK, now I see what you wanted to do, probably better described as:

            "I want to call a member function on each element of an array of object
            pointers."

            In the case of most operators it's often best to write them as free
            functions rather than a member functions, anyway. (usually this becomes
            apparent when your operator can also deal with another type).

            bool operator<(const myclass& c, int i) {
            /* something */
            }

            bool operator<(int i, const myclass& c) {
            return c>i;
            }

            Allows for:
            myclass c;
            int i;
            c < i
            as well as
            i < c;

            Should it be required.

            In any case, if an operation can be performed using the public interface
            of a class, make it a free function. No need to clutter the interface
            of a class unnecessarily.

            Ben Pope
            --
            I'm not just a number. To many, I'm known as a string...

            Comment

            • Daniel T.

              #7
              Re: Class pointer vector and STL

              In article <1140451694.534 922.231350@f14g 2000cwb.googleg roups.com>,
              "Alex" <acamposr@gmail .com> wrote:
              [color=blue]
              > Hello. I would like a simpler way to do something like this:
              >
              > class C {
              > ...
              > bool operator<(const C&) const;
              > };
              >
              > int main()
              > {
              > std::vector<C*> v;
              > ...
              > C* x = max_element(v.b egin(),v.end()) ;[/color]

              You forgot to dereference the iterator above...
              [color=blue]
              > }
              >
              > without having to write this:
              >
              > bool operator<(const C* a, const C* b)
              > { return *a<*b; }[/color]

              I don't know if you would consider this simpler. :-)

              class C { };
              bool operator<( const C& lhs, const C& rhs );

              int main() {
              vector<C*> v;
              C* x = *max_element( v.begin(), v.end(),
              f_gx_gy( less<C>(), ptr_deref<C>() ) );
              }

              The above calls, ptr_deref<C>() on two C* objects, then sends the C
              objects they contain to less<C>() for comparison...

              To do the above, you need:

              template <typename T>
              struct ptr_deref: public std::unary_func tion<T*,T>
              {
              T& operator()(T* t ) const {
              return *t;
              }
              };

              template <typename Op1, typename Op2>
              class f_gx_gy_t: public std::binary_fun ction<
              typename Op2::argument_t ype, typename Op2::argument_t ype,
              typename Op1::result_typ e>
              {
              Op1 fn1;
              Op2 fn2;
              public:
              f_gx_gy_t() { }
              f_gx_gy_t(const Op1& f, const Op2& g): fn1(f), fn2(g) { }

              typename Op1::result_typ e operator()(
              const typename Op2::argument_t ype& x,
              const typename Op2::argument_t ype& y) const
              {
              return fn1(fn2(x), fn2(y));
              }
              };

              template <typename Op1, typename Op2>
              inline f_gx_gy_t<Op1, Op2, Op2> f_gx_gy(const Op1& f, const Op2& g) {
              return f_gx_hy_t<Op1, Op2>(f, g);
              }

              Yes, you have these two classes and a function but it saves you quite a
              bit because you can build any function that compares two pointers...

              f_gx_gy( equal_to<C>(), ptr_deref<C>() );
              f_gx_gy( not_equal_to<C> (), ptr_deref<C>() ) );
              f_gx_gy( greater<C>(), ptr_deref<C>() ) );

              and so on...

              --
              Magic depends on tradition and belief. It does not welcome observation,
              nor does it profit by experiment. On the other hand, science is based
              on experience; it is open to correction by observation and experiment.

              Comment

              • Daniel T.

                #8
                Re: Class pointer vector and STL

                In article <1140453283.593 745.127680@g14g 2000cwa.googleg roups.com>,
                "Alex" <acamposr@gmail .com> wrote:
                [color=blue]
                > I want to know if there is some function in the STL to apply a class
                > method to an array of pointers to that class.
                >
                > Something similar to mem_fun, but for class pointers.[/color]

                mem_fun *is* for pointers... But it has nothing to do with
                max_element... Now I have no idea what you are asking for.


                --
                Magic depends on tradition and belief. It does not welcome observation,
                nor does it profit by experiment. On the other hand, science is based
                on experience; it is open to correction by observation and experiment.

                Comment

                Working...