Sorting a container of Type*! 
Author Message
 Sorting a container of Type*!

Hi,

I have a vector container of type MyObject* which is instantiated as such :

    vector<MyObject*> vecMyObject;

I then add a MyObject pointer to my vector as such :

    MyObject* pMyObect = new MyObject;
    vecMyObject.push_back( pMyObject );

Now the question is : how do I sort the container not by pointer order but
by order of the underlying MyObject objects?  I have written operator<() and
operator>() for the MyObject class.

I have found a solution to the problem but I am not sure if it is the best
one.  What I have done is written two function object classes for the
greater than and the less than operations that I pass as the Compare
parameter in the generic sort( ) function.  These function objects then
dereference the incoming pointer arguments and then return the result of the
comparison.

I am curious to know if there is a better way to do this?

Thanks,

Louis Carlino



Sun, 22 Apr 2001 03:00:00 GMT  
 Sorting a container of Type*!
A better way to do this is to write a template class that holds a
pointer - something like:
template<class _T>
class Pointer   {
        _T* ptr;
public:
        const _T& operator*() const
        {
                return *ptr;
        }

        const _T *operator->() const
        {
                return ptr;
        };

Quote:
};

Then you need to have global functions - for e.g.:
// Less than operator
template <class _T>
bool operator < (const Pointer <_T>& lhs, const Pointer <_T>& rhs)
{
        return *lhs < *rhs;  // Note the dereferencing of the
pointers

Quote:
};

Now you create a collection of Pointers:
typedef Pointer<MyObject *> MyObjectPtr;
vector<MyObjectPtr> vec;

You add a MyObject pointer to the vector:
    MyObject* pMyObect = new MyObject;
    vec.push_back( Pointer<pMyObject> );

Finally, you can sort by using:
sort(vec.begin(), vec.end());

Note that the default < opertor for the MyObject class is called due to
the global function earlier that dereferences pointers.

If you want to sort by >, then you need to have a similar operator for >
that dereferences the pointers. Code above is untested, but you get the
idea.

Hope this helps,
Vasi

Quote:
-----Original Message-----

Posted At: Wednesday, November 04, 1998 6:12 PM
Posted To: stl
Conversation: Sorting a container of Type*!
Subject: Sorting a container of Type*!

Hi,

I have a vector container of type MyObject* which is instantiated as
such :

    vector<MyObject*> vecMyObject;

I then add a MyObject pointer to my vector as such :

    MyObject* pMyObect = new MyObject;
    vecMyObject.push_back( pMyObject );

Now the question is : how do I sort the container not by pointer order
but
by order of the underlying MyObject objects?  I have written operator<()
and
operator>() for the MyObject class.

I have found a solution to the problem but I am not sure if it is the
best
one.  What I have done is written two function object classes for the
greater than and the less than operations that I pass as the Compare
parameter in the generic sort( ) function.  These function objects then
dereference the incoming pointer arguments and then return the result of
the
comparison.

I am curious to know if there is a better way to do this?

Thanks,

Louis Carlino



Sun, 22 Apr 2001 03:00:00 GMT  
 Sorting a container of Type*!

Quote:
>pointer - something like:
>template<class _T>
>class Pointer {
> _T* ptr;
>public:

Names with an underscore followed by a capital letter are reserved for the
implementation (as are names with two underscores in them). The "_T"
identifier could be renamed "Type".

Mario Contestabile



Thu, 26 Apr 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sorting Containers (such as CMap or CVector classes)

2. Non-sorted associative containers?

3. Question on sorting of containers

4. List STL container with user defined types

5. VC++ 5.0: STL containers do not compile with nested types

6. Container Class w different type entries

7. Advise on type of STL container needed - PleaseHelp!

8. STL container class specialization for pointer types -- when?

9. sorting techniques for differnt data types?

10. hi,who can help me how to sort files by file type

11. Having a container vested in another container

12. OLE Server Container fast switching of document and container crashes

 

 
Powered by phpBB® Forum Software