I am trying to find the most efficient, optimized and fastest way to compare to std vectors of CString. the strings in question are case-sensitive. I have tried using the == operator for the vector container but this sometimes return false positives. I mean for instance if one vector contains elements in the order (a,b,c) and the other has them in the order (b,c,a) the == operator will return false even thought they share the same data. Another thing is that it does not do case sensitive comparison.
I have thought of using a basic nested loops approach like this:
//Not Tested
BOOL bMatch = TRUE;
for(int i=0; i<Vec1.size();i++)
{
if(!bMatch)
break;
int nComp=0;
for(int j=0;j<Vec2.size();j++)
{
if(vec1[i].CompareNoCase(Vec2[j])==0)
{
//We have a match--check next item
break;
}
else
{
nComp++;
if(nComp == Vec2.size()-1)
{
//Reached end of vector and no match found
//Vectors don't match
bMatch=FALSE;
}
}
}
}
The above code is not tested and I am not sure if there is probably a better way to achieve such comparison without the need of using nested loops.
Would appreciate any advice or help…
Simply insert the data into two containers where the order does not matter and compare those:
If you want to ignore the case (which the code in your question seems to suggest), you can parameterize
std::multisetandstd::equalwith an appropriate comparator:The parameterization of
std::multisetguarantees that “hello” and “HELLO” in the same vector are treated as one value, and the parameterization ofstd::equalguarantees this across the two vectors.And finally, if you know that no element occurs twice in the same
vector, you can usesetinstead ofmultiset. Note that it’s probably better to work with asetormultisetright from the start.