CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2005
    Posts
    23

    Problem With STL List Sort

    Hello,

    I am having a problem sorting a large list. Below is an example.

    Code:
    #include <iostream>
    #include <list>
    using namespace std;
    
    void main()
    {
    
    	list<int> ints;
    	for(int k=0;k<50000;k++) ints.push_back(k);
    	ints.sort();
    	cout << "ints.size(): " << ints.size() << endl;
    }
    The number of integers left in ints is 17232, meaning some were lost during ints.sort(). I would like to know what the cause of the problem is and the sorting algorithm used in sort(). Thank you for this information or a link to it.

    dremmuel

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Problem With STL List Sort

    Works fine in Visual Studio 2005.

  3. #3
    Join Date
    Mar 2005
    Posts
    23

    Re: Problem With STL List Sort

    Maybe it's just an issue with Visual Studio 6.0. I was able to sort the list by sorting parts of it and merging the parts together. I suppose it will have to do until I can get my hands on Visual Studio 2005.

    Thanks for the information John.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Problem With STL List Sort

    I just tried it in Visual Studio 6.0 and got the same results as you.

    The maximum number of elements it can sort seems to be 32767, which is the maximum positive value of a 16bit signed integer.

    It appears to roll-over its count after 32767

    50000 - 32768 = 17232
    Last edited by JohnW@Wessex; February 20th, 2008 at 12:28 PM.

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Problem With STL List Sort

    See "FIX: Listing Sort Function Removes Elements" at http://support.microsoft.com/kb/240014

    It's an "off by one" coding error. Fixed in Visual C++ .NET.

    Scary, isn't it?

    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured