• ### Popular Now

• 12
• 12
• 9
• 10
• 13

#### Archived

This topic is now archived and is closed to further replies.

# how to sort an STL container with pointers?

This topic is 5274 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i have an STL container (list, but i could use something else if necessary) full of pointers to objects. i want to sort it, but apparently when i call the "sort" function it is sorting by the pointers'' values (memopry addresses), not the objects pointed to. is there a way to get it to dereference the pointers for the sort call? i could get around it, but it would be a messy slow hack...

##### Share on other sites
Not tested this.. but somrthing like

bool pointercompare(const your_pointer_type* p1, const your_pointer_type* p2){return *p1 < * p2;}int main(){...std::sort(something.begin(), something.end(), pointercompare);...}

##### Share on other sites
Make your own functor to pass to the sort function. Something like:

template<typename T>class pointer_less{public:    bool operator() (const T *t1, const T *T2) { return *t1 < *t2; }};std::list<int *> myList;...std::sort(myList.begin(), myList.end(), pointer_less<int>());

How appropriate. You fight like a cow.

##### Share on other sites
excellent, thank you!

##### Share on other sites
sneftel (or anyone else who knows all about these things):

i tried that, but i get the warning:
"template argument RandomAccessIterator passed to ''sort'' is a bi-directional iterator: random iterator required in function CTerrain::doPath()"

and then 16 or so other errors in "algorithm.cc" and ".h", relating to missing operators (+/-) for the iterators...

it also has a missing "copy_backwards", "std::__median", "__unguarded_partition", etc... i assume these are all things relating to it expecting a RandomAccessIterator; if so, how would i get one of those from ".begin()"?

##### Share on other sites
Wow, what STL library are you using? (If it''s true, that is an excellent diagnostic)

Are you attempting to sort a map or list? Map''s are stored sorted, and list have a member function to sort them.

##### Share on other sites
quote:
Original post by krez
i tried that, but i get the warning:
"template argument RandomAccessIterator passed to ''sort'' is a bi-directional iterator: random iterator required in function CTerrain::doPath()"
(.. snip)
how would i get one of those from ".begin()"?

quote:
my documentation
random access iterators permit values to be accessed by subscript, subtracted one from another (to yield the number of elements between their respective values), or modified by arithmetic operations, all in a manner similar to conventional pointers.

if you''re using a std::list, you simply cannot get a random access iterator. std::sort works with random access iterators. that''s just "The Way It Is" (.. if you want to know why, it''s for performance reasons).

so, it stands that you can''t use std::sort with a list::iterator - this is by design.

use the list::sort() member function, instead.

##### Share on other sites
quote:
Original post by Magmai Kai Holmlor
Wow, what STL library are you using? (If it''s true, that is an excellent diagnostic)

um... whatever comes with the borland command-line compiler?
quote:
Are you attempting to sort a map or list? Map''s are stored sorted, and list have a member function to sort them.

yah, it''s a std::list...
quote:
Original post by Anonymous Poster
if you''re using a std::list, you simply cannot get a random access iterator.
...
use the list::sort() member function, instead.

alright, thanks!

i tried that to begin with, since i saw it had one, but that gave me errors as well... i''ll be back in a few minutes with an update.

##### Share on other sites
with the std::list member function sort, i get a different error ("cannot find match for blah blah blah...")... which means that i am getting one or more of the arguments wrong, i believe.

i tried with both the compare function (first AP), and the functor thing from sneftel. the comparator function compiles. but i get warnings about comparing signed and unsigned values (which i definitely am not doing)...

so, what is the proper syntax to use this function? i must be making a stupid mistake here or something...

##### Share on other sites
kinda sleepy but I'll give this a shot in the dark

#include <list>#include <iostream>#include <algorithm> // needed for the for_each algorithm#include <stdlib.h>using namespace std;// just a simple functor for comparing pointers,// since the operator () is templated you don't have// to care about what kind of pointer is passed to it.struct dereference_less{  template <class T>  bool operator () (const T* lhs, const T* rhs) const  {    return *lhs < *rhs;  }};// just a simple functor for cout'ing pointers,// since the operator () is templated you don't have// to care about what kind of pointer is passed to it.struct dereference_cout{  template <class T>  void operator () (T* tp)  {    cout << *tp;  }};int main(){  list<int*> l;  for(int i=0; i < 10; i++)    l.push_back( new int( 9-i ) );  cout << "List created: ";  for_each(l.begin(), l.end(), dereference_cout());  cout << endl;  l.sort( dereference_less() );  cout << "The list is now sorted: ";  for_each(l.begin(), l.end(), dereference_cout());  cout << endl;    std::cin.get();  return 0;}

I ran it and it works fine!

To tired to make it better... goonight

}-- Programmer/Gamer/Dreamer --{

[edited by - Seriema on October 11, 2003 9:33:34 AM]