Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Sorting a list in VC++

Sorting a list in VC++

Scheduled Pinned Locked Moved C / C++ / MFC
c++algorithms
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anderson Jogie
    wrote on last edited by
    #1

    I am trying to sort a list using the insertion sort algo, but my application crashes. I think those pointers lost track by using the remove function. Is there a better way to sort except using the sort() (list.sort() function). It does not work too well with it since I am sorting by Departure Time of the object in the list. Here is the code i wrote: //Insertion sort to sort runs by departure time in the Res_RunList int numruns = Res_RunList.size(); //the size of the list ListOfRuns::iterator Tempitr1; //create itr poniters to navigate thru the list ListOfRuns::iterator Tempitr2; ListOfRuns::iterator Tempitr3; ListOfRuns::iterator Tempitr4; ListOfRuns::iterator Tempitr5; ListOfRuns::iterator Tempitr6; std::list <RDLRun*> TempList; ListOfRuns::iterator Tempitr7; Tempitr1 = Res_RunList.begin(); //points to the 1st element Tempitr2 = Res_RunList.begin(); Tempitr6 = Res_RunList.begin(); for(int low = 1; low < numruns; ++low) { ++Tempitr2; //points to low position RDLRun *TempRun = (*Tempitr2); // store the data from low position in TempRun int u = low -1; Tempitr3 = Tempitr2; Tempitr4 = Tempitr3--; //ponits to u position //compare the time windows while(u >=0 && ( (*Tempitr4)->GetDepartureTime() > TempRun->GetDepartureTime() ) ) { Tempitr5 = Tempitr4; Res_RunList.insert(Tempitr5++, (*Tempitr4)); Res_RunList.remove((*Tempitr5++));//to prevent duplicate --u; --Tempitr3; }//end while loop if(u > 0 || TempRun->GetDepartureTime() > (*Tempitr1)->GetDepartureTime()) { Res_RunList.insert(Tempitr4++, TempRun); Res_RunList.remove((*Tempitr4++)); } else{ Res_RunList.insert(Tempitr6++, (*Tempitr1)); Res_RunList.remove((*Tempitr6++)); Res_RunList.insert(Tempitr1, TempRun); } }//end for loop Thanks. ;)

    _ 2 Replies Last reply
    0
    • A Anderson Jogie

      I am trying to sort a list using the insertion sort algo, but my application crashes. I think those pointers lost track by using the remove function. Is there a better way to sort except using the sort() (list.sort() function). It does not work too well with it since I am sorting by Departure Time of the object in the list. Here is the code i wrote: //Insertion sort to sort runs by departure time in the Res_RunList int numruns = Res_RunList.size(); //the size of the list ListOfRuns::iterator Tempitr1; //create itr poniters to navigate thru the list ListOfRuns::iterator Tempitr2; ListOfRuns::iterator Tempitr3; ListOfRuns::iterator Tempitr4; ListOfRuns::iterator Tempitr5; ListOfRuns::iterator Tempitr6; std::list <RDLRun*> TempList; ListOfRuns::iterator Tempitr7; Tempitr1 = Res_RunList.begin(); //points to the 1st element Tempitr2 = Res_RunList.begin(); Tempitr6 = Res_RunList.begin(); for(int low = 1; low < numruns; ++low) { ++Tempitr2; //points to low position RDLRun *TempRun = (*Tempitr2); // store the data from low position in TempRun int u = low -1; Tempitr3 = Tempitr2; Tempitr4 = Tempitr3--; //ponits to u position //compare the time windows while(u >=0 && ( (*Tempitr4)->GetDepartureTime() > TempRun->GetDepartureTime() ) ) { Tempitr5 = Tempitr4; Res_RunList.insert(Tempitr5++, (*Tempitr4)); Res_RunList.remove((*Tempitr5++));//to prevent duplicate --u; --Tempitr3; }//end while loop if(u > 0 || TempRun->GetDepartureTime() > (*Tempitr1)->GetDepartureTime()) { Res_RunList.insert(Tempitr4++, TempRun); Res_RunList.remove((*Tempitr4++)); } else{ Res_RunList.insert(Tempitr6++, (*Tempitr1)); Res_RunList.remove((*Tempitr6++)); Res_RunList.insert(Tempitr1, TempRun); } }//end for loop Thanks. ;)

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      I don't see you using sort in your code. Also, any insertion or deletion in the list renders the iterators invalid. This can give you unexpected results.

      «_Superman_» I love work. It gives me something to do between weekends.
      Microsoft MVP (Visual C++)

      A 1 Reply Last reply
      0
      • _ _Superman_

        I don't see you using sort in your code. Also, any insertion or deletion in the list renders the iterators invalid. This can give you unexpected results.

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        A Offline
        A Offline
        Anderson Jogie
        wrote on last edited by
        #3

        Do you know of any better way to approach this scenario? Sort those objects in acending order by within a list? Thanks.

        1 Reply Last reply
        0
        • A Anderson Jogie

          I am trying to sort a list using the insertion sort algo, but my application crashes. I think those pointers lost track by using the remove function. Is there a better way to sort except using the sort() (list.sort() function). It does not work too well with it since I am sorting by Departure Time of the object in the list. Here is the code i wrote: //Insertion sort to sort runs by departure time in the Res_RunList int numruns = Res_RunList.size(); //the size of the list ListOfRuns::iterator Tempitr1; //create itr poniters to navigate thru the list ListOfRuns::iterator Tempitr2; ListOfRuns::iterator Tempitr3; ListOfRuns::iterator Tempitr4; ListOfRuns::iterator Tempitr5; ListOfRuns::iterator Tempitr6; std::list <RDLRun*> TempList; ListOfRuns::iterator Tempitr7; Tempitr1 = Res_RunList.begin(); //points to the 1st element Tempitr2 = Res_RunList.begin(); Tempitr6 = Res_RunList.begin(); for(int low = 1; low < numruns; ++low) { ++Tempitr2; //points to low position RDLRun *TempRun = (*Tempitr2); // store the data from low position in TempRun int u = low -1; Tempitr3 = Tempitr2; Tempitr4 = Tempitr3--; //ponits to u position //compare the time windows while(u >=0 && ( (*Tempitr4)->GetDepartureTime() > TempRun->GetDepartureTime() ) ) { Tempitr5 = Tempitr4; Res_RunList.insert(Tempitr5++, (*Tempitr4)); Res_RunList.remove((*Tempitr5++));//to prevent duplicate --u; --Tempitr3; }//end while loop if(u > 0 || TempRun->GetDepartureTime() > (*Tempitr1)->GetDepartureTime()) { Res_RunList.insert(Tempitr4++, TempRun); Res_RunList.remove((*Tempitr4++)); } else{ Res_RunList.insert(Tempitr6++, (*Tempitr1)); Res_RunList.remove((*Tempitr6++)); Res_RunList.insert(Tempitr1, TempRun); } }//end for loop Thanks. ;)

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          The best approach is to use TempList.sort(MyCompare()); Here MyCompare is a functor used to decide how to sort the list of RDLRun class pointers.

          «_Superman_» I love work. It gives me something to do between weekends.
          Microsoft MVP (Visual C++)

          A 1 Reply Last reply
          0
          • _ _Superman_

            The best approach is to use TempList.sort(MyCompare()); Here MyCompare is a functor used to decide how to sort the list of RDLRun class pointers.

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            A Offline
            A Offline
            Anderson Jogie
            wrote on last edited by
            #5

            Thanks for the insight. I have create a little functor to work with the sort function but getting an error. there is something that I am not doing right. Thanks for your help. :) Functor code: bool RDLTemporaryResource::SortByTime:public std::binary_function<RDLRun*, RDLRun*, bool> { bool operator() (RDLRun* a, RDLRun* b) const { if( a->GetDepartureTime() < b->GetDepartureTime() ) return 1; else return 0; } } Compiler error message: --------------------Configuration: fstool - Win32 Debug-------------------- Compiling... TEMPRES.CPP c:\program files\ilps\code\fstool\tempres.cpp(837) : fatal error C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1786) Please choose the Technical Support command on the Visual C++ Help menu, or open the Technical Support help file for more information Error executing cl.exe. fstool.dll - 1 error(s), 0 warning(s)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups