比较 CString 的两个向量的最佳方法是什么
我正在尝试找到与 CString 的 std 向量进行比较的最有效、最优化和最快的方法。有问题的字符串区分大小写。我尝试过对向量容器使用 == 运算符,但这有时会返回误报。我的意思是,例如,如果一个向量包含顺序为 (a,b,c) 的元素,而另一个向量包含顺序为 (b,c,a) 的元素,则 == 运算符将返回 false,即使它们共享相同的数据。另一件事是它不进行区分大小写的比较。
我想过使用像这样的基本嵌套循环方法:
//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;
}
}
}
}
上面的代码没有经过测试,我不确定是否有更好的方法来实现这种比较而不需要使用嵌套循环。
将不胜感激任何建议或帮助...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需将数据插入到顺序无关紧要的两个容器中并进行比较:
如果您想忽略大小写(问题中的代码似乎建议这样做),您可以参数化
std::multiset
和带有适当比较器的std::equal
:std::multiset
的参数化保证“hello”和“HELLO”在相同中向量被视为一个值,并且 std::equal 的参数化在两个向量中保证了这一点。最后,如果您知道同一
向量
中没有元素出现两次,则可以使用set
而不是multiset
。请注意,从一开始就使用set
或multiset
可能会更好。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::multiset
andstd::equal
with an appropriate comparator:The parameterization of
std::multiset
guarantees that "hello" and "HELLO" in the same vector are treated as one value, and the parameterization ofstd::equal
guarantees this across the two vectors.And finally, if you know that no element occurs twice in the same
vector
, you can useset
instead ofmultiset
. Note that it's probably better to work with aset
ormultiset
right from the start.如果 (a,b,c) 和 (b,c,a) 对您来说是相同的,那么向量是一个糟糕的选择,请使用
std::set
或std::multiset 相反,并且如前所述,将它们与 std::equal 进行比较,并传递 strcmp 作为比较器参数。如果 CString 指的是 C 风格的 null 终止字符数组,那么这个答案是有效的。如果CString意味着MFC CString,那么FredOverflow的答案是完美的。
if (a,b,c) and (b,c,a) are the same for you then vector is a bad choice, use
std::set
orstd::multiset
instead, and, as already said, compare them withstd::equal
and passstrcmp
as the comparator argument. This answer is valid if by CString you mean C-style null-terminated char arrays. If CString means MFC CString, FredOverflow's answer is perfect.首先使用 std::sort 对它们进行排序,然后使用 std::equal 进行比较。
Sort them first with std::sort, then compare them with std::equal.
不要使用简单的 for 循环。相反,您可以使用迭代器从两个向量中检索元素,然后使用 _tcscmp 或 wcscmp 比较值。
Dont't use simple for loop. Instead you can use iterators to retrieve the elements from both vectors and then compare the values either using _tcscmp or wcscmp.