How to use find algorithm with a vector of pointers to objects in c++?

后端 未结 4 2080
逝去的感伤
逝去的感伤 2021-02-07 07:04

I want to find in a vector of Object pointers for a matching object. Here\'s a sample code to illustrate my problem:

class A {
public:
    A(string a):_a(a) {}
          


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 08:07

    Either use std::find_if and provide a suitable predicate yourself, see other answers for an example of this.

    Or as an alternative have a look at boost::ptr_vector, which provides transparent reference access to elements which are really stored as pointers (as an extra bonus, memory management is handled for you as well)

提交回复
热议问题