ネタ元 → VB.NET に匿名メソッドが欲しい~
匿名メソッド欲しいよね... C++にもありません。
C++には「指定した条件を満たす要素をさがしてコイ」
って関数find_ifてのがあります。
template<typename Iterator, typename Predicate>
Iterator find_if(Iterator first, Iterator last, Predicate pred);
-- first以上last未満の範囲にあるxに対し、pred(*x) が真なるxを返す。
これ使って検索すると、こんな感じ:
#include <iostream> // cout, endl
#include <vector> // vector
#include <algorithm> // find_if
#include <string> // string
using namespace std;
int main() {
const char* names[] = {
"中", "えムナウ", "επιστημη", "けろ",
"とりこびと", "とっちゃん", "夏椰", "なおこ" };
vector<string> wankuma(names, names+8);
vector<string>::iterator found;
// コンストラクタで与えた文字列と一致すればtrue
// を返す operator() を持ったオブジェクト
struct filter {
string target;
filter(const string& t) : target(t) {}
bool operator()(const string& x) const { return x == target; }
};
// filterを与えて検索する
found = find_if(wankuma.begin(), wankuma.end(),
filter("επιστημη"));
if ( found != wankuma.end() ) cout << "あったー!" << endl;
}