code>与一个合适的函子: struct Foo { int i; }; bool operator ==(const Foo& lhs,const Foo& rhs){return lhs.i == rhs.i; } std :: vector< Foo *> v = .... Foo f {42}; std :: find_if(v.begin(),v.end(),[& f](const Foo * p){return * p == f;}); I have a class A with a member which is a vector of object pointers of another class Bclass A{ std::vector<B*> m_member_ANow I want to perform a std::find on m_member_A.E.g.if(std::find(m_member_A.begin(), m_member_A.end(), B_obj*) != m_member_A.end())std::find doesn't make sense on such a vector. How do I achieve such a functionality?How would it change if it were a vector of objects of B (not pointer)? 解决方案 If you want to find a value pointer at by the pointer, instead of a given pointer, you can use std::find_if with a suitable functor:struct Foo{ int i;};bool operator==(const Foo& lhs, const Foo& rhs) { return lhs.i == rhs.i; }std::vector<Foo*> v = ....;Foo f{42};std::find_if(v.begin(), v.end(), [&f](const Foo* p) { return *p == f; }); 这篇关于std :: find对象指针的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-31 20:27