けっこう便利
公開メンバ変数検索用
template <class Iterator, class Target, class Type, class ClassName> inline Iterator mem_find(Iterator first, Iterator last, const Target& target, Type ClassName::*member) { while (first != last) { if ((*first).*member == target) break; ++first; } return first; }
使い方 ↓↓↓↓↓↓↓↓
struct hoge { string name_; int age_; hoge(const string& name, int age) : name_(name), age_(age) {} }; int main() { vector<hoge> v; v.push_back(hoge("Johnny", 39)); v.push_back(hoge("Akira", 22)); v.push_back(hoge("Sum", 58)); if (mem_find(v.begin(), v.end(), "Akira", &hoge::name_) != v.end()) cout << "該当あり" << endl; else cout << "該当なし" << endl; return 0; }
こんなループ処理書くよりラクでしょ
for (vector<hoge>::iterator first = v.begin(), last = v.end(); first != last; ++first) { if (first->name == "Akira") // 該当あり...ループ抜ける } // 該当なし