list:: unique and sort wouldn't work 
Author Message
 list:: unique and sort wouldn't work

Hi,

I have the following:

list<char *> *lt;

lt->push_back(somedata);
....

When, I try to :
lt->sort();
lt->unique();
the list wouldn't be sorted and I will still have
duplicates.

I also tried:
lt->sort(greater<char *>() );
lt->unique(not_equal_to<char *> () );

Still, wouldn't work.

Please, can someone help me to sort and remove duplicates
from the list?

Thanks a lot!



Sat, 15 May 2004 02:24:20 GMT  
 list:: unique and sort wouldn't work
It IS sorted, only the comparison function is not the one you think it is.
Suppose p and q are members of your list (both are char*). Then, when you
sort the list, it is reordered so that p precedes q in the list when p < q
(compared as pointers). You probably expected strcmp to automatically get
into the picture somehow. Well, it does not.

Use list<string>. std::string has operator< defined appropriately.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat, and
wrong." H.L. Mencken


Quote:
> Hi,

> I have the following:

> list<char *> *lt;

> lt->push_back(somedata);
> ....

> When, I try to :
> lt->sort();
> lt->unique();
> the list wouldn't be sorted and I will still have
> duplicates.

> I also tried:
> lt->sort(greater<char *>() );
> lt->unique(not_equal_to<char *> () );

> Still, wouldn't work.

> Please, can someone help me to sort and remove duplicates
> from the list?

> Thanks a lot!



Sat, 15 May 2004 02:37:12 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. size limit in list-sort-unique or am I messed up

2. list::sort(operatorFunction) does not work

3. The Thread That Wouldn't Die

4. vc wouldn't initialize static members in static libraries

5. VC5: IDE reuses old source-window and wouldn't open new ones

6. Sort doesn't work on bound data

7. Why doesn't SORT work?

8. sort() doesn't work with pred class in vc 6

9. Why doesn't SORT work?

10. using std::list's sort member function

11. Why can't I sort this list?

12. Sorting array with non-unique key and qsort

 

 
Powered by phpBB® Forum Software