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)
  ....
}

私の2つの醜いループではなく、stdまたはboost関数でそれを行う良い方法があるかどうか疑問に思っています。ありがとう!

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 に答える