比较 CString 的两个向量的最佳方法是什么

发布于 2024-09-28 19:00:06 字数 856 浏览 1 评论 0原文

我正在尝试找到与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的往事 2024-10-05 19:00:06

如果一个向量包含顺序为 (a,b,c) 的元素,而另一个向量包含顺序为 (b,c,a) 的元素,则 == 运算符将返回 false,即使它们共享相同的数据。

只需将数据插入到顺序无关紧要的两个容器中并进行比较:

std::vector<CString> vec1;
std::vector<CString> vec2;

// ...

std::multiset<CString> set1(vec1.begin(), vec1.end());
std::multiset<CString> set2(vec2.begin(), vec2.end());

bool equal_data = (set1 == set2);

如果您想忽略大小写(问题中的代码似乎建议这样做),您可以参数化 std::multiset和带有适当比较器的 std::equal

struct compareNoCase
{
    bool operator()(const CString& a, const CString& b)
    {
        return a.CompareNoCase(b);
    }
};

std::vector<CString> vec1;
std::vector<CString> vec2;

// ...

std::multiset<CString> set1(vec1.begin(), vec1.end(), compareNoCase());
std::multiset<CString> set2(vec2.begin(), vec2.end(), compareNoCase());

bool equal_data = std::equal(set1.begin(), set1.end(),
                             set2.begin(),
                             compareNoCase());

std::multiset 的参数化保证“hello”和“HELLO”在相同中向量被视为一个值,并且 std::equal 的参数化在两个向量中保证了这一点。

最后,如果您知道同一向量中没有元素出现两次,则可以使用set而不是multiset。请注意,从一开始就使用 setmultiset 可能会更好。

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.

Simply insert the data into two containers where the order does not matter and compare those:

std::vector<CString> vec1;
std::vector<CString> vec2;

// ...

std::multiset<CString> set1(vec1.begin(), vec1.end());
std::multiset<CString> set2(vec2.begin(), vec2.end());

bool equal_data = (set1 == set2);

If you want to ignore the case (which the code in your question seems to suggest), you can parameterize std::multiset and std::equal with an appropriate comparator:

struct compareNoCase
{
    bool operator()(const CString& a, const CString& b)
    {
        return a.CompareNoCase(b);
    }
};

std::vector<CString> vec1;
std::vector<CString> vec2;

// ...

std::multiset<CString> set1(vec1.begin(), vec1.end(), compareNoCase());
std::multiset<CString> set2(vec2.begin(), vec2.end(), compareNoCase());

bool equal_data = std::equal(set1.begin(), set1.end(),
                             set2.begin(),
                             compareNoCase());

The parameterization of std::multiset guarantees that "hello" and "HELLO" in the same vector are treated as one value, and the parameterization of std::equal guarantees this across the two vectors.

And finally, if you know that no element occurs twice in the same vector, you can use set instead of multiset. Note that it's probably better to work with a set or multiset right from the start.

拧巴小姐 2024-10-05 19:00:06

如果 (a,b,c) 和 (b,c,a) 对您来说是相同的,那么向量是一个糟糕的选择,请使用 std::setstd::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 or std::multiset instead, and, as already said, compare them with std::equal and pass strcmp 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.

孤独患者 2024-10-05 19:00:06

首先使用 std::sort 对它们进行排序,然后使用 std::equal 进行比较。

Sort them first with std::sort, then compare them with std::equal.

寻找我们的幸福 2024-10-05 19:00:06

不要使用简单的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文