Getting "Debugging confirmation failed!" for comparator

I know that this topic was answered via this link. Help me fix this std :: set C ++ compiler but, unfortunately, I faced the same problem and I can not understand the reason for this, so you need help in its elimination.

I am using VS2010 and my release binary is working fine, without any problems, but debugging binary reports:

enter image description here

My comparator is as follows:

struct PathComp { bool operator() (const wchar_t* path1, const wchar_t* path2) const { int c = wcscmp(path1, path2); if (c < 0 || c > 0) { return true; } return false; } }; 

My recruitment is declared as follows:

 set<wchar_t*,PathComp> pathSet; 

Can anyone suggest me why my debug binary does not work with this statement? Is it because I use the wcscmp () function to compare a large string of characters stored in my set?

Thanks in advance!

+7
source share
3 answers

std::set requires a valid compiler that behaves like operator< or std::less .

The std :: set code found that your statement <is invalid, and the confirmation you showed is called as help.

And really: your compiler looks like operator!= , And not like operator<

One of the rules a operator< must follow is that a<b and b<a cannot be true. In your implementation of this.

Correct your code:

 bool operator() (const wchar_t* path1, const wchar_t* path2) const { int c = wcscmp(path1, path2); return (c < 0); } 

and everything should be fine.

+15
source

The problem is that your comparator does not cause strict weak ordering. It should only truly return true for paths that are "smaller" - not for all that are different from each other. Change it to:

 struct PathComp { bool operator() (const wchar_t* path1, const wchar_t* path2) const { int c = wcscmp(path1, path2); if (c < 0) { // <- this is different return true; } return false; } }; 

Also, using only c > 0 will also work, but the set will be in reverse order.

The algorithm should know the difference between smaller and larger work, it simply does not provide enough information unevenly. Without information, less than / more than a set, a set cannot maintain order - but that's what it is about.

+9
source

After spending some more time on this, we finally decided to use a different approach that worked for me.

So, we converted wchar_t * to a string using this method:

 // Converts LPWSTR to string bool convertLPWSTRToString(string& str, const LPWSTR wStr) { bool b = false; char* p = 0; int bSize; // get the required buffer size in bytes bSize = WideCharToMultiByte(CP_UTF8, 0, wStr,-1, 0,0, NULL,NULL); if (bSize > 0) { p = new char[bSize]; int rc = WideCharToMultiByte(CP_UTF8, 0, wStr,-1, p,bSize, NULL,NULL); if (rc != 0) { p[bSize-1] = '\0'; str = p; b = true; } } delete [] p; return b; } 

And then I saved this line in the set, having done this, I didn’t have to worry about comparing the elements that are stored to make sure that all records are unique.

 // set that will hold unique path set<string> strSet; 

So all I had to do was:

 string str; convertLPWSTRToString(str, FileName); // store path in the set strSet.insert(str); 

Although I still don’t know what caused the "Debug with error" error when I used a set comparator (PathComp) for set<wchar_t*,PathComp> pathSet;

+1
source

All Articles