CString 找到最后一个条目

2022-01-12 00:00:00 c++ mfc

我有两个 CString s1CString s2.我需要在 s1 中找到最后一个条目 s2.我可以在 CString 中找到任何方法,例如在 C# LastIndexOf 中.我是 C++ 中的菜鸟.提前致谢.

I have two CString s1 and CString s2. I need find the last entry s2 in s1. I can find any metod in CString like in C# LastIndexOf. I am nooby in c++. Thanks in advance.

推荐答案

CString 没有这个功能.你必须自己写,例如

CString has no such function. You have to write it yourself, e.g.

int LastIndexOf(const CString& s1, const CString& s2)
  {
  int found = -1;
  int next_pos = 0;
  for (;;)
    {
    next_pos = s1.Find(s2, next_pos);
    if (next_pos == -1)
      return found;

    found = next_pos;
    };
  }

一种更优化的算法会首先反转字符串,我将其留作练习.

A more optimal algorithm would reverse the strings first, I'm leaving that as an exercise.

相关文章