我正试图找到一种优雅的方式来找到一些文字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或boost函数,而不是我的2个丑陋的循环。谢谢!
答案 0 :(得分:5)
您可以将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*)
。
答案 1 :(得分:0)
您可以查看std::search
。
string pattern[] = {"hello","world"};
list<string>::iterator it = search(x.begin(),x.end(), pattern, pattern + 1);
x
是您的清单。您可能必须提供自己的二元谓词。