Class pointer vector and STL

A

Alex

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.begin(),v.end());
}

without having to write this:

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

Ben Pope

Alex said:
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.begin(),v.end());
}

without having to write this:

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

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
 
D

Dietmar Kuehl

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

What exactly do you mean by "simpler"? Less repetitive lines of
code?
std::vector<C*> v;
...
C* x = max_element(v.begin(),v.end());

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

without having to write this:

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

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 "dereference" 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).
 
A

Alex

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.
 
B

Ben Pope

Alex said:
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.

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
 
D

Daniel T.

"Alex said:
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.begin(),v.end());

You forgot to dereference the iterator above...
}

without having to write this:

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

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_function<T*,T>
{
T& operator()(T* t ) const {
return *t;
}
};

template <typename Op1, typename Op2>
class f_gx_gy_t: public std::binary_function<
typename Op2::argument_type, typename Op2::argument_type,
typename Op1::result_type>
{
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_type operator()(
const typename Op2::argument_type& x,
const typename Op2::argument_type& 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...
 
D

Daniel T.

"Alex said:
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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top