473,406 Members | 2,633 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,406 software developers and data experts.

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

without having to write this:

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

Feb 20 '06 #1
7 2012
Alex wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 20 '06 #2
Alex wrote:
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:

std::vector<C*>::iterator it
= std::max_element(v.begin(), v.end(), ...);
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).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 20 '06 #3
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.

Feb 20 '06 #4
mem_fun_t is exactly what I was looking for. Thanks anyway.

Feb 20 '06 #5
Alex wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 20 '06 #6
In article <11**********************@f14g2000cwb.googlegroups .com>,
"Alex" <ac******@gmail.com> wrote:
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...

--
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.
Feb 20 '06 #7
In article <11**********************@g14g2000cwa.googlegroups .com>,
"Alex" <ac******@gmail.com> wrote:
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.
--
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.
Feb 20 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steven Lien | last post by:
I wrote a simple vector class that can store Cards, and my question is if i had Card class, should i "new" it to store into vector , or simply use copy structure?. if i need the vector class to be...
1
by: Augasm | last post by:
I'm trying to clean up a bunch of my code by building C++ classes for commonly used code elements, and have run into a problem. Allow me to preface this with a disclaimer that I am not formally...
20
by: Joe Van Dyk | last post by:
Is there some rule of thumb about when to use pointers to an object and when to use a reference* to an object when a class needs to have objects as data members? Example: class A { B* b_ptr;...
7
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want...
16
by: Manuel | last post by:
hi, In the past I made the question "how to implement a simple class forname". I made this finally and it compiled well. but now when i execute the program, it crash with a sigsegv. The code...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
6
by: chris.kemmerer | last post by:
I am having a problem with templates and I hope someone here can help. I am writing a library that accepts data packets, parses them and saves the information for later use. One member of the...
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.