how to sort an STL container with pointers?

Started by
14 comments, last by krez 20 years, 6 months ago
Well, this is off the top of my head, so there'll most likely be a few errors - I have no compiler on this 'puter. You might have to play around with it.

For the comparison:
class pointer_less{public:   template <typename T>   bool operator()(const T* lhs, const T* rhs) const {      return *lhs < *rhs;   }};

Something like this for generic outputting:
template <typename CharT = char, typename Traits = std::char_traits<CharT> >class pointer_print{public:   typedef std::basic_ostream<CharT, Traits>   ostream_type;   typedef std::basic_string<CharT, Traits>    string_type; public:   pointer_print(ostream_type& out)    : os_(out) {}   pointer_print(ostream_type& out, const string_type& separate)    : os_(out), sep_(separate) {}   pointer_print(const pointer_print& rhs)    : os_(rhs.os_), sep_(rhs.sep_) {}    template <typename T>   void operator()(const T* obj) {      os_ << *obj << sep_;   } private:   ostream_type& os_;   string_type sep_;};

For deleting the objects from the list:
class single_delete{public:   template <typename T>   void operator()(T*& ptr) const {      delete ptr;      ptr = 0;   }};

And a little example test program:
int main(){   std::list<int*> L;   for (int i = 10; i > 0; --i)      L.push_back( new int(i) );    std::cout << "Initial list:" << std::endl << std::endl;   std::for_each( L.begin(), L.end(), pointer_print(std::cout, "\n") );    L.sort( pointer_less() );    std::cout << std::endl             << "Sorted list: " << std::endl << std::endl;   std::for_each( L.begin(), L.end(), pointer_print(std::cout, "\n") );    std::for_each( L.begin(), L.end(), single_delete() );   L.clear();    std::cout << "Press any key to continue...";   std::cin.get();   return 0;}


Edit: Something tells me the compiler wouldn't like "templaete".

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 12, 2003 4:29:27 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Advertisement
*applause*

Very good Lektrix!
I was to tired to do all that "reusable" stuff

}-- Programmer/Gamer/Dreamer --{
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
ah, after sleeping and typing it again from scratch it works, thanks to you useful peoples... thanks a lot!
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Oh, and here's a version of pointer_print that you can also use like an std::ostream_iterator, but for pointers (you could use it with std::copy).
template <typename CharT = char, typename Traits = std::char_traits<CharT> >class pointer_print{public:   typedef std::basic_ostream<CharT, Traits>   ostream_type;   typedef std::basic_string<CharT, Traits>    string_type; public:   pointer_print(ostream_type& out)    : os_(out) {}   pointer_print(ostream_type& out, const string_type& separate)    : os_(out), sep_(separate) {}   pointer_print(const pointer_print& rhs)    : os_(rhs.os_), sep_(rhs.sep_) {}    template <typename T>   void operator()(const T* obj) {      os_ << *obj << sep_;   }    template <typename T>   pointer_print& operator=(const T* obj) {      os_ << *obj << sep_;      return *this;   }    pointer_print& operator*() {      return *this;   }    pointer_print& operator++() {      return *this;   }    pointer_print& operator++(int) {      return *this;   } private:   ostream_type& os_;   string_type sep_;};


[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on October 12, 2003 4:30:04 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
I think that the borland bcc comes with STLport from memory (i know it does in 6 not sure about the 5.5 version that came with the command line compiler)

quote:
um... whatever comes with the borland command-line compiler?

well, at the top of "vector.h" it is copyrighted by Hewlett-Packard (1994) and Rogue Wave Software(1994-99)...
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])

This topic is closed to new replies.

Advertisement