文字列をオブジェクトのメンバーフィールドと比較することにより、ベクトル内の特定のオブジェクトのインデックスを見つけるための優れた方法を見つけようとしています。
このような:
find(vector.begin(), vector.end(), [object where obj.getName() == myString])
私は成功せずに検索しました-多分私は何を探すべきか完全に理解していません。
std::find_if
適切なファンクターで使用できます。この例では、C++11ラムダが使用されています。
std::vector<Type> v = ....;
std::string myString = ....;
auto it = find_if(v.begin(), v.end(), [&myString](const Type& obj) {return obj.getName() == myString;})
if (it != v.end())
{
// found element. it is an iterator to the first matching element.
// if you really need the index, you can also get it:
auto index = std::distance(v.begin(), it);
}
C ++ 11ラムダをサポートしていない場合は、ファンクターが機能します。
struct MatchString
{
MatchString(const std::string& s) : s_(s) {}
bool operator()(const Type& obj) const
{
return obj.getName() == s_;
}
private:
const std::string& s_;
};
ここで、MatchString
は、インスタンスが単一のType
オブジェクトで呼び出し可能であり、ブール値を返す型です。例えば、
Type t("Foo"); // assume this means t.getName() is "Foo"
MatchString m("Foo");
bool b = m(t); // b is true
次に、インスタンスをに渡すことができますstd::find
std::vector<Type>::iterator it = find_if(v.begin(), v.end(), MatchString(myString));
juanchoが使用するLambdaと手書きのファンクターに加えて、boost::bind
(C ++ 03)またはstd::bind
(C ++ 11)と単純な関数を使用する可能性があります。
bool isNameOfObj(const std::string& s, const Type& obj)
{ return obj.getName() == s; }
//...
std::vector<Type>::iterator it = find_if(v.begin(), v.end(),
boost::bind(&isNameOfObj, myString, boost::placeholders::_1));
または、Type
メソッドがある場合isName
:
std::vector<Type>::iterator it = find_if(v.begin(), v.end(),
boost::bind(&Type::isName, boost::placeholders::_1, myString));
これは完全を期すためのものです。C ++ 11ではLambdasを好み、C++03では比較関数自体がすでに存在する場合にのみbindを使用します。そうでない場合は、ファンクターをお勧めします。
PS: C ++ 11にはポリモーフィック/テンプレート化されたラムダがないため、bindはC ++ 11でもその場所にあります。たとえば、パラメータータイプが不明な場合、スペルが難しい場合、または推測が難しい場合などです。
単純なイテレータが役立つ場合があります。
typedef std::vector<MyDataType> MyDataTypeList;
// MyDataType findIt should have been defined and assigned
MyDataTypeList m_MyObjects;
//By this time, the push_back() calls should have happened
MyDataTypeList::iterator itr = m_MyObjects.begin();
while (itr != m_MyObjects.end())
{
if(m_MyObjects[*itr] == findIt) // any other comparator you may want to use
// do what ever you like
}