C++ 如何将贴图中的元素删除为向量

C++ 如何将贴图中的元素删除为向量,c++,c++14,stdvector,unordered-map,C++,C++14,Stdvector,Unordered Map,我目前正在尝试一个游戏项目,当一架飞机在到达预定到达的终点站之前坠毁时,它将摆脱一个悬而未决的参考 我只想通过函数来更好地了解它们是如何工作的 目前,我已尝试浏览包含与端子关联的所有平面的地图,将其与所有平面的列表进行比较,并检查地图中的平面是否不再位于矢量中,然后将其从地图中删除以释放关联端子 void remove_crashed_aircraft(std::unordered_map<const Aircraft*, size_t>& reserved_terminal

我目前正在尝试一个游戏项目,当一架飞机在到达预定到达的终点站之前坠毁时,它将摆脱一个悬而未决的参考

我只想通过
函数来更好地了解它们是如何工作的

目前,我已尝试浏览包含与端子关联的所有平面的地图,将其与所有平面的列表进行比较,并检查地图中的平面是否不再位于矢量中,然后将其从地图中删除以释放关联端子

void remove_crashed_aircraft(std::unordered_map<const Aircraft*, size_t>& reserved_terminals, std::vector<std::unique_ptr<Aircraft>>& aircrafts)
{
    auto it = std::all_of(reserved_terminals.begin(), reserved_terminals.end(), 
        [aircrafts](const Aircraft* a1){ return std::find_if(aircrafts.begin(), aircrafts.end(),
            [a1](std::unique_ptr<Aircraft> a2){ return a1==a2.get();});});
    
    reserved_terminals.erase(it);
}
void remove\u坠毁的飞机(标准::无序地图和保留终端,标准::矢量和飞机)
{
auto it=std::所有(保留的\u端子.begin(),保留的\u端子.end(),
[aircrafts](const-Aircraft*a1){return std::find_if(aircrafts.begin(),aircrafts.end(),
[a1](std::unique_ptr a2){return a1==a2.get();});});
保留_终端。擦除(it);
}
这是我的飞机等级:

class Aircraft : public GL::Displayable, public GL::DynamicObject
{
private:
    const AircraftType& type;
    const std::string flight_number;
    Point3D pos, speed; // note: the speed should always be normalized to length 'speed'
    WaypointQueue waypoints = {};
    Tower& control;
    bool landing_gear_deployed = false; // is the landing gear deployed?
    bool is_at_terminal        = false;
    int fuel                   = 0;

    // turn the aircraft to arrive at the next waypoint
    // try to facilitate reaching the waypoint after the next by facing the
    // right way to this end, we try to face the point Z on the line spanned by
    // the next two waypoints such that Z's distance to the next waypoint is
    // half our distance so: |w1 - pos| = d and [w1 - w2].normalize() = W and Z
    // = w1 + W*d/2
    void turn_to_waypoint();
    void turn(Point3D direction);

    // select the correct tile in the plane texture (series of 8 sprites facing
    // [North, NW, W, SW, S, SE, E, NE])
    unsigned int get_speed_octant() const;
    // when we arrive at a terminal, signal the tower
    void arrive_at_terminal();
    // deploy and retract landing gear depending on next waypoints
    void operate_landing_gear();
    void add_waypoint(const Waypoint& wp, const bool front);
    bool is_on_ground() const { return pos.z() < DISTANCE_THRESHOLD; }
    float max_speed() const { return is_on_ground() ? type.max_ground_speed : type.max_air_speed; }
    bool is_paused = false;

    Aircraft(const Aircraft&) = delete;
    Aircraft& operator=(const Aircraft&) = delete;

public:
    Aircraft(const AircraftType& type_, const std::string_view& flight_number_, const Point3D& pos_,
             const Point3D& speed_, Tower& control_, int fuel_) :
        GL::Displayable { pos_.x() + pos_.y() },
        type { type_ },
        flight_number { flight_number_ },
        pos { pos_ },
        speed { speed_ },
        control { control_ },
        fuel { fuel_ }
    {
        speed.cap_length(max_speed());
    }

    const std::string& get_flight_num() const { return flight_number; }
    float distance_to(const Point3D& p) const { return pos.distance_to(p); }
    bool is_circling() const 
    {
        if (!has_terminal() && !is_at_terminal)
        {
            return true;
        }
        return false;
    }
    bool has_terminal() const 
    { 
        if (waypoints.empty())
        {
            return false;
        }
        else
        {
            return waypoints.back().type == wp_terminal;
        } 
    }
    bool is_low_on_fuel() const 
    {
        if (fuel<200)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    void display() const override;
    bool move() override;
    void refill(int& fuel_stock);

    friend class Tower;
    friend class AircraftManager;
};
class飞机:公共总账::可显示,公共总账::动态对象
{
私人:
const飞机型号和型号;
const std::字符串航班号;
Point3D pos,speed;//注意:速度应始终标准化为长度“speed”
航路点队列航路点={};
塔台与控制;
bool起落架部署=false;//起落架是否部署?
bool在终端处=false;
int燃料=0;
//转动飞机到达下一个航路点
//尽量面向下一个航路点后到达该航路点
//在正确的方向上,我们试着面对由
//下两个航路点,以便Z到下一个航路点的距离为
//我们距离的一半是:| w1-pos |=d和[w1-w2]。normalize()=W和Z
//=w1+W*d/2
无效转弯至航路点();
无效转弯(点3D方向);
//在平面纹理中选择正确的瓷砖(一系列8个精灵朝向
//[北部、西北部、西部、西南部、南部、东南部、东部、东北部])
无符号整数get_speed_octant()常量;
//当我们到达终点站时,向塔台发出信号
无效到达_终端();
//根据下一个航路点展开和收回起落架
无效操作起落架();
无效添加航路点(恒定航路点和wp,恒定边界前方);
布尔是地面上的常数{return pos.z()bool
,而不是迭代器。但是std::remove\u如果您需要,它将删除所有匹配bool谓词的实例

其次,出现编译错误是因为您在任何地方都按值传递,所以不是按
(std::unique\u ptr a2)
传递引用
(std::unique\u ptr const&a2)
,而是按
[飞机]
[飞机]
传递

第三,内部谓词不应只返回
std::find_if
(返回迭代器)的结果,而应返回
bool
,即将find_if的结果与
aircrafts.end()进行比较

在速度方面,如果需要,您的算法可以进行优化,因为您必须将std::vector转换为std::unordered_集,然后遍历第一个映射并检查第二个映射集中的包含情况。如果您没有太多元素,并且速度不太重要,那么您的算法就可以了

最终工作代码如下:

void remove_crashed_aircraft(
    std::unordered_map<const Aircraft*, size_t>& reserved_terminals,
    std::vector<std::unique_ptr<Aircraft>>& aircrafts
) {
    std::remove_if(reserved_terminals.begin(), reserved_terminals.end(), 
        [&aircrafts](auto const & a1){
            return std::find_if(aircrafts.begin(), aircrafts.end(),
                [&a1](auto const & a2){ return a1.first == a2.get(); })
                == aircrafts.end();
        }
    );
}
void移除坠毁的飞机(
std::无序地图和保留终端,
标准::矢量和飞机
) {
std::删除_if(保留_端子.begin(),保留_端子.end(),
[&aircrafts](自动常数和a1){
return std::find_if(aircrafts.begin(),aircrafts.end(),
[&a1](自动常量&a2){返回a1.first==a2.get();})
==飞机。结束();
}
);
}

在调用
std::find_if()
时,尝试代替
(std::unique_ptr a2)
传递引用
(std::unique_ptr const&a2)
调用
std::find_if()
。这有帮助吗?“已删除函数的使用”将被取消,但静态断言仍然失败,我不知道为什么还要将
[飞机]
替换为
[飞机]
。它是否修复了错误?
void remove_crashed_aircraft(
    std::unordered_map<const Aircraft*, size_t>& reserved_terminals,
    std::vector<std::unique_ptr<Aircraft>>& aircrafts
) {
    std::remove_if(reserved_terminals.begin(), reserved_terminals.end(), 
        [&aircrafts](auto const & a1){
            return std::find_if(aircrafts.begin(), aircrafts.end(),
                [&a1](auto const & a2){ return a1.first == a2.get(); })
                == aircrafts.end();
        }
    );
}