Click here to Skip to main content
15,888,454 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
Questionsorting an STL list of =references= to objects Pin
kerchunk1-Dec-09 12:39
kerchunk1-Dec-09 12:39 
AnswerRe: sorting an STL list of =references= to objects Pin
kerchunk1-Dec-09 14:44
kerchunk1-Dec-09 14:44 
GeneralRe: sorting an STL list of =references= to objects Pin
Simple Inheritance1-Dec-09 16:48
Simple Inheritance1-Dec-09 16:48 
GeneralRe: sorting an STL list of =references= to objects Pin
kerchunk1-Dec-09 20:56
kerchunk1-Dec-09 20:56 
GeneralRe: sorting an STL list of =references= to objects Pin
kerchunk2-Dec-09 10:02
kerchunk2-Dec-09 10:02 
GeneralRe: sorting an STL list of =references= to objects [modified] Pin
Simple Inheritance2-Dec-09 10:48
Simple Inheritance2-Dec-09 10:48 
GeneralRe: sorting an STL list of =references= to objects Pin
kerchunk3-Dec-09 19:42
kerchunk3-Dec-09 19:42 
GeneralRe: sorting an STL list of =references= to objects Pin
Simple Inheritance4-Dec-09 10:38
Simple Inheritance4-Dec-09 10:38 
BUG: The STL list::sort() Function Doesn't Sort a List of Pointers
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q265109
The Standard Template Library (STL) list::sort function doesn't sort a list of pointers when a predicate function is defined for sorting.

VC6 is a ten-year old compiler now. The bug is not present in VC2008 or MinGW. Your code is implemented below with the workaround described in KB265109.
________________________________________________________________________________________

#include <iostream>
#include <string>
#include <list>

using namespace std;

struct pstchip_pin_wor
{
      string primitive;
      string pin_name;
      string pin_number;

      pstchip_pin_wor( string aa, string bb, string cc)
      {
            primitive   = aa;
            pin_name   = bb;
          pin_number = cc;
     }

      pstchip_pin_wor& pstchip_pin_wor::operator= (const pstchip_pin_wor &itemToCopy)
      {
            if(&itemToCopy != this)
          {
               this->primitive   = itemToCopy.primitive;
               this->pin_name   = itemToCopy.pin_name;
               this->pin_number = itemToCopy.pin_number;
          }

          return *this;
     }

     int pstchip_pin_wor::operator == ( const pstchip_pin_wor& itemToCompare) const
     {
          if( this->primitive == itemToCompare.primitive   && this->pin_name   < itemToCompare.pin_name )
               return 1;
          return 0;
     }

     int pstchip_pin_wor::operator   < ( const pstchip_pin_wor& itemToCompare) const
     {
          return Compare(this, &itemToCompare);
     }

     static bool Compare(const pstchip_pin_wor* elem1, const pstchip_pin_wor* elem2)
     {
          if( elem1->primitive   < elem2->primitive )
               return 1;
          if( elem1->primitive == elem2->primitive && elem1->pin_name < elem2->pin_name)
               return 1;

          return 0;
     }
};

/* ADDED ACCORDING TO KB265109 */
template<>
struct std::greater<pstchip_pin_wor*> : public binary_function<pstchip_pin_wor* ,pstchip_pin_wor*, bool>
{
      bool operator()(const pstchip_pin_wor* &elem1, const pstchip_pin_wor* &elem2) const
      {
          return pstchip_pin_wor::Compare(elem1, elem2);
      };
};
/* ADDED ACCORDING TO KB265109 */

struct SortFunc
/* ADDED ACCORDING TO KB265109 */
: public greater<pstchip_pin_wor*>
/* ADDED ACCORDING TO KB265109 */
{
     bool operator()(pstchip_pin_wor*& elem1, pstchip_pin_wor*& elem2)
     {
          return pstchip_pin_wor::Compare(elem1, elem2);
     }
};


int main(int argc, char* argv[])
{
     int nRetCode = 0;

     list<pstchip_pin_wor*>   wors_pstchip_pins;

     wors_pstchip_pins.push_back( new pstchip_pin_wor( "Europe",            "Spain",   "Madrid" ) );
     wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "USA",      "Washington DC" ) );
     wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "Canada",   "Ottawa" ) );
     wors_pstchip_pins.push_back( new pstchip_pin_wor( "Europe",            "Germany", "Berlin" ) );
     wors_pstchip_pins.push_back( new pstchip_pin_wor( "South America", "Brazil",   "Sao Paulo" ) );
     wors_pstchip_pins.push_back( new pstchip_pin_wor( "North America", "Mexico",   "Mexico City" ) );

     cout << "before sorting" << endl << endl;
     for( list<pstchip_pin_wor*>::iterator ka = wors_pstchip_pins.begin(); ka != wors_pstchip_pins.end(); ka++ )
     {cout << (*ka)->primitive << "==" << (*ka)->pin_name << "==" << (*ka)->pin_number << endl;}

     wors_pstchip_pins.sort(SortFunc());

     cout << endl << "after   sorting" << endl << endl;
     for( list<pstchip_pin_wor*>::iterator zm = wors_pstchip_pins.begin(); zm != wors_pstchip_pins.end(); zm++ )
     {cout << (*zm)->primitive << "==" << (*zm)->pin_name << "==" << (*zm)->pin_number << endl;}

     return nRetCode;
}
GeneralRe: sorting an STL list of =references= to objects Pin
kerchunk6-Dec-09 9:03
kerchunk6-Dec-09 9:03 
QuestionHow to get clipping region of windowless activex? Pin
smalti1-Dec-09 9:15
smalti1-Dec-09 9:15 
QuestionError on using BMP image in LoadBitmap Pin
am 200926-Nov-09 0:47
am 200926-Nov-09 0:47 
AnswerRe: Error on using BMP image in LoadBitmap Pin
KingsGambit29-Nov-09 19:16
KingsGambit29-Nov-09 19:16 
QuestionHow to control IWMPPlayer? Pin
kcynic25-Nov-09 16:30
kcynic25-Nov-09 16:30 
QuestionNo Keyboard Support for ActiveX Controls in WIN32 App Pin
hZenz23-Nov-09 0:12
hZenz23-Nov-09 0:12 
QuestionHow to implement a multi-column menu? Pin
hawkgao012919-Nov-09 21:00
hawkgao012919-Nov-09 21:00 
AnswerRe: How to implement a multi-column menu? Pin
hawkgao012919-Nov-09 21:58
hawkgao012919-Nov-09 21:58 
QuestionExposing methods via a COM object Pin
mbet87817-Nov-09 22:24
mbet87817-Nov-09 22:24 
AnswerRe: Exposing methods via a COM object Pin
Jonathan Davies18-Nov-09 7:42
Jonathan Davies18-Nov-09 7:42 
AnswerRe: Exposing methods via a COM object Pin
Lim Bio Liong18-Nov-09 22:59
Lim Bio Liong18-Nov-09 22:59 
AnswerRe: Exposing methods via a COM object Pin
mbet87825-Nov-09 23:09
mbet87825-Nov-09 23:09 
QuestionHow to prevent Quicksort stack overflow? Pin
crazy6612-Nov-09 6:21
crazy6612-Nov-09 6:21 
AnswerRe: How to prevent Quicksort stack overflow? Pin
Richard MacCutchan12-Nov-09 9:14
mveRichard MacCutchan12-Nov-09 9:14 
QuestionERROR - Object deleted before window was destroyed Pin
kcynic12-Nov-09 1:23
kcynic12-Nov-09 1:23 
AnswerRe: ERROR - Object deleted before window was destroyed Pin
Stuart Dootson12-Nov-09 5:03
professionalStuart Dootson12-Nov-09 5:03 
GeneralRe: ERROR - Object deleted before window was destroyed Pin
kcynic12-Nov-09 14:00
kcynic12-Nov-09 14:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.