我有一个指向带有建筑物的Vector的指针。

vector<building> * building1;
building1 = gamer.getBuilding(); ( building1 is a pointer to vector with all buildings that gamer has on that moment. )

现在,我要检查该 vector 中是否存在称为房屋的建筑物。

我以为我可以做类似的事情
vector<building>::iterator it;

it = find((*building1).begin(), (*building1).end(),buildings::house);

其中建筑物是枚举。

但这行不通。

有人能帮我吗?

亲切的问候,

最佳答案

答案取决于building的定义,您不会显示。但通常,当您不想按值而是按谓词查找时,可以使用find_if:

struct building_of_type
{
public:
    explicit building_of_type( buildings type ) : _type( type ){}

    bool operator ()( building const& b ) const {  return is b of type _type?; }
private:
    buildings const _type;
};

std::find_if(
    building1->begin(), building1->end()
  , building_of_type( buildings::house )
);

或更简单的情况:
bool is_building_a_house( building const& b ){ return is b of type house?; }

std::find_if(
    building1->begin(), building1->end()
  , is_building_a_house
);

09-05 06:34