我试图找出一种在向量中查找某个对象的索引的好方法——通过将字符串与对象中的成员字段进行比较。
像这样:
find(vector.begin(), vector.end(), [object where obj.getName() == myString])
我已经搜索但没有成功 - 也许我不完全了解要寻找什么。
您可以使用std::find_if
合适的函子。在此示例中,使用了 C++11 lambda:
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 lambda 支持,则仿函数可以工作:
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));
除了 Lambda 和 juancho 使用的手写函子之外,您还可以使用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 中我只在比较函数本身已经存在时才使用绑定。如果不是,请选择仿函数。
PS:由于 C++11 没有多态/模板化的 lambda,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
}