视觉C++查找以 "ing" 结尾且少于或等于 10 个字符的单词

visual C++ find words ending with "ing" and are less than or equal to 10 characters

本文关键字:字符 单词 查找 C++ ing 结尾 视觉      更新时间:2023-10-16

正如标题所示,我想在我的向量中找到所有符合此规则的单词。

我当前的代码是:

void Dictionary::endingIng() {
string findIng;
string foundIng;
int sevenLetters = 7;
for (Word findIng : deffinitions)
    if (findIng.getWord().find("ing") <= sevenLetters) {
        foundIng = findIng.getWord();
        cout << foundIng << endl;
    }

但是,它还返回中间某处包含"ing"的单词和长度超过 10 个字符的单词,例如:

accordingly
barringout
bingo
commandingly

感谢您的帮助。

也许这会有所帮助。添加一个案例以跳过超过 10 个字母的单词。

还要检查单词 <= 10 个字母的"ing"位置。

rfind用于边缘情况,如"inginging"

for (Word findIng : deffinitions) {
   std::string word = findIng.getWord();
   if (word.length() > 10) {
      // skip this - word is more than 10 letters including "ing"
      continue;
   }
   size_t pos = word.rfind("ing");
   if (pos == word.length() - 3) {
      // we got a match "ing" as suffix of this word
      // do something with `word`
   }
}

您为什么不(使用当前解决方案(检查长度,然后获取最后 3 个值并检查它们是否匹配。

或者你可以去

CString findString = "ing";
int maxLetters = findString.GetLength() + 7; //magic number
foreach(CString s in dictionary)
{        
    bool b_SmallEnough = (s.Find(findString) <= maxLetters );
    bool b_EndsIn = (s.ReverseFind(findString) == (s.GetLength() - findString.GetLength()); //reverse find so it doesn't get the first "ing in bringing and return false because 2 != 5
    if(b_SmallEnough && b_EndsIn)
    {
        std::wcout << s << std::endl;
    }
}

或者,布尔b_EndsIn可以是:

bool b_EndsIn = (s.MakeReverse.Find(findString.MakeReverse()) == 0);

这意味着您首先检查结束,并且始终获得最后一场比赛