如何迭代指向 Class 的共享指针向量

How to iterate over vector of shared pointers to a Class

我有一个 class:

class Company {
    public:
    int floors;
    std::set<std::string> managers;
};

另一个 class 具有以下内容:

class Another {
    std::vector<std::shared_ptr<Company>> comp;
    }

如果我有这个功能需要查找是否有任何经理在 comp 中的任何公司 classes 中有名字,我如何遍历 comp 向量,特别是它的经理在 comp?

中为每个公司设置
bool Another::look_for(std::string name);

应该 return 如果 comp 中的任何经理有 'name'

感谢您的帮助

bool Another::look_for(std::string name)
{
    for (auto &c : comp) {
        if (c->managers.find(name) != c->managers.end())
            return true;
    }
    return false;
}

或者:

#include <algorithm>

bool Another::look_for(std::string name)
{
    return std::find_if(
        comp.begin(), comp.end(),
        [&](auto &c) { return c->managers.find(name) != c->managers.end(); }
    ) != comp.end();
}