Sign in to follow this  
paynechu

problem in std::sort with self define compare function (VC6)

Recommended Posts

see the code below, The compare function got a dirty pointer when it swapped the elements. The error also about the size of vector. it will occurs the error when the size more than 20. what's the problem? is it the VC6 standard library bug? and how to fix it? #include "stdafx.h" #include <vector> #include <algorithm> #include <tchar.h> #include <windows.h> typedef struct _SIMInfo { BOOL bOnline; } SIMInfo; std::vector<SIMInfo*> m_arr; bool CompareFriend(SIMInfo * infox, SIMInfo * infoy) { bool b = infox->bOnline; bool b2 = infoy->bOnline; return true; } int main(int argc, char* argv[]) { for (int i = 0; i < 50; ++i) { SIMInfo * pinfo = new SIMInfo; memset(pinfo, 0, sizeof(SIMInfo)); m_arr.push_back(pinfo); } std::sort(m_arr.begin(), m_arr.end(), CompareFriend); return 0; }

Share this post


Link to post
Share on other sites
I've discovered this with VC6's STL as well. If your compare function ever returns true for both a < b and b < a, then it can cause a fatal error. You can fix it by making sure your compare function makes sense :)

Share this post


Link to post
Share on other sites
Errrr. I would rewrite your compare function like this:

bool CompareFriend(const SIMInfo * infox, const SIMInfo * infoy)
{
bool b = infox->bOnline;
bool b2 = infoy->bOnline;
return b < b2;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oh thank you~
my original compare function is below

bool CompareFriend(SIMInfo * infox, SIMInfo * infoy)
{
if (infox->bOnline && !infoy->bOnline)
return true;
else if (!infox->bOnline && infoy->bOnline)
return false;
else
{
int nCmpResult = _tcscmp(infox->szName, infoy->szName);
if (nCmpResult > 0)
return false;
else if (nCmpResult <

Share this post


Link to post
Share on other sites
oh thank you~
my original compare function is below

<textarea>
bool CompareFriend(SIMInfo * infox, SIMInfo * infoy)
{
if (infox->bOnline && !infoy->bOnline)
return true;
else if (!infox->bOnline && infoy->bOnline)
return false;
else
{
int nCmpResult = _tcscmp(infox->szName, infoy->szName);
if (nCmpResult > 0)
return false;
else if (nCmpResult < 0)
return true;
else
return true; <- the problem is here, should instead to false
}
}
</textarea>

Share this post


Link to post
Share on other sites
Please log in and use [source][/source] tags.
Quote:
return true; <- the problem is here, should instead to false
The comment you have made on one of the lines is indeed correct. It must be false there because you're supposed to only return true when infox is less than infoy. When they are equal, they are not less than.

Try using this:
bool CompareFriend(SIMInfo * infox, SIMInfo * infoy)
{
if (infox->bOnline && !infoy->bOnline)
return true;
else if (!infox->bOnline && infoy->bOnline)
return false;
else
return (_tcscmp(infox->szName, infoy->szName) < 0);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this