したがって、ベクター内のオブジェクトの文字列を「検索」できるようにする次のコードがあります。
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
struct migObj
{
migObj(const std::string& a_name, const std::string& a_location) : name(a_name), location(a_location) {}
std::string name;
std::string location;
};
int main()
{
typedef std::vector<migObj> migVec;
migVec v;
v.push_back(migObj("fred", "belfast"));
v.push_back(migObj("ivor", "london"));
// Search by name.
const std::string name_to_find = "ivor";
auto i = std::find_if(v.begin(), v.end(), [&](const migObj& obj) { return name_to_find == obj.name;});
if (i != v.end())
{
std::cout << i->name << ", " << i->location << "\n";
}
return 0;
}
「名前で検索」というコマンドの下のコードが、この結果に関与しています。私がやりたいことは、std::vector または typedef migVec に追加されるメソッドを作成することです。
v.myNewSearchFunction("ivor");