c++ - 如何使 String::Find(is) 省略这个

标签 c++ string find

如果我有一个列表,其中包含 4 个节点(“this”;“test example”;“is something of”;“a small”)并且我想找到每个包含“is”的字符串(只有 1 个对这份 list 持肯定态度)。这个主题已经发布了很多次,我用它来帮助我走到这一步。但是,我在任何地方都看不到我是如何从阳性结果中省略“这个”的。我可能会使用 string::c_str,然后在我减少了更大的列表之后自己找到它。或者我可以使用 string::find_first_of 吗?似乎有更好的方法。谢谢。
编辑:我知道我可以省略一个特定的字符串,但我正在寻找更大的图片 b/c 我的列表非常大(例如:诗歌)。

for(it = phrases.begin(); it != phrases.end(); ++it)
{
    found = it->find(look);
    if(found != string::npos)
        cout << i++ << ". " << *it << endl;
    else
    {
        i++;
        insert++;
    }
}

最佳答案

澄清一下:你在为什么而苦苦挣扎?

你要做的是检查你找到的是否是一个词(或短语)的开头,也是一个词(或短语)的结尾

即。检查是否:

  • found 等于 phrases.begin 或者 found 之前的元素是一个空格
  • AND found 之后的两个元素是一个空格 OR phrases.end

编辑:您可以访问使用 found 找到的字符(将 X 替换为您要查找的字符串的长度 (look.length)

found = it->find(look);
if(found!=string::npos)
{
    if((found==0 || it->at(found-1)==' ')
        && (found==it->length-X || it->at(found+X)==' '))
    {
         // Actually found it
    }
} else {
    // Do whatever
}

关于c++ - 如何使 String::Find(is) 省略这个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20367854/

相关文章:

c++ - std::common_type 应该使用 std::decay 吗?

c++ - 从另一个线程调用 socket->native_handle()

jQuery 从字符串中删除 '-' 字符

javascript - 使用 javascript 在 Div 中查找 anchor

c++ - rand() 有时会连续返回相同的结果吗?

.net - 使用 __declspec(thread) 的线程本地存储在 C++/CLI 中失败

python - 如何使用 Python 中的键访问 JSON 字符串中的值?

c# - 将 linq 查询转换为字符串数组 - C#

linux - 跨多个文件增量查找和替换 - Bash

C++ 映射从迭代器位置开始搜索