C++列表中<word>查找句子
C++ find a sentence in list<word>
我试图找到一种优雅的方式来找到一些文本ex。 "hello world"
从句子"I compiled my first hello world. It works!"
但是这句话是一个带有一些元数据的std::list<word>
。
Class Word
{
std::string m_word;
Location ... (X,Y in a picture)
....
}
只是想知道是否有很好的方法可以通过一些 std 或提升功能而不是我的 2 个丑陋循环来做到这一点。谢谢!
可以将
std::search
与仅比较m_word
成员的自定义谓词一起使用:
bool wordsEqual(const Word& a, const Word& b) {
return a.getWord() == b.getWord();
}
// ...
Word needle[] = { "hello", "world" };
list<Word>::iterator it = search(lst.begin(), lst.end(),
needle, needle + 2,
wordsEqual);
此代码假定数组初始化的 getWord
方法和构造函数Word(const char*)
。
你可以查看std::search
.
string pattern[] = {"hello","world"};
list<string>::iterator it = search(x.begin(),x.end(), pattern, pattern + 1);
x
在哪里是你的列表。您可能需要提供自己的二进制谓词。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 在C++中查找文件
- 模板元程序查找相似的连续类型名称
- 在UNIX系统中使用DIR查找文件的字节大小
- 查找最接近的大于当前数字的数字的索引
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 查找后更改类变量
- 使用正则表达式regex_search在字符串中查找字符串
- 使用gcc从静态链接的文件中查找可选符号
- 在C++中查找范围的长度
- 算法问题:查找从堆栈中弹出的所有序列
- 在Windows中查找扬声器输出的当前音量级别
- 如何在C++中使用X509证书模在令牌中查找私钥
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- EASTL矢量<向量<int>>连续的
- 在 for 循环中查找问题时遇到困难
- 如何在文件中查找字节序列
- 试图创建一个多线程程序来查找0-100000000之间的总素数
- 使用堆查找第K个最大元素的时间复杂性