2

我正在尝试找到一种优雅的方式来查找一些文本。"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 个丑陋的循环。谢谢!

4

2 回答 2

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*)函数。

于 2012-08-20T22:14:59.953 回答
0

你可以调查一下std::search

string pattern[] = {"hello","world"};
list<string>::iterator it = search(x.begin(),x.end(), pattern, pattern + 1);

x你的清单在哪里。您可能必须提供自己的二元谓词。

于 2012-08-20T22:14:00.437 回答