插入到包含另一个结构的structur中并从中删除元素

insert in and delete an element from structur containing another structure

本文关键字:删除 元素 structur 包含 另一个 结构 插入      更新时间:2023-10-16

我有一个包含另一个结构的结构,我想从第一个结构中删除一个元素。这两个结构是这样定义的:1**第一种结构:

struct Solution {
    Problem *_Problem;
    vector<Tour> _tour; .
    vector<unsigned int> length; 
    unsigned int nbrCleint; 
    unsigned int TotalDemande;
}

2**第二种结构:

struct Tour{
    vector<unsigned int> clientVisite;
    unsigned int demande; 
    vector<unsigned int> CostScenario;
    Tour(); 
    void clear();
};

现在我想从结构中删除"解决方案"中的一个元素;例如delete元素,其中解决方案_tour.clientVisite=="某个矢量"

我该怎么做?如何插入具有相同原理的元素??

注意C++<C++11

Solution s;
for (vector<Tour>::iterator i = s._tour.begin(); i != s._tour.end(); ++i) {
  if (i->clientVisite == somevector) {
    s._tour.erase(i); break;
  }
}
struct Solution {
    /// Remove every tour where the tour's visit array exactly matches visit
    /// @param visit is a vector of unsigned ints representsing locations
    /// @returns the number of tours removed
    std::size_t remove_tour_where_visit_matches(const std::vector<unsigned int> visit)
    {
        auto original = _tour.size();
        _tour.erase(std::remove_if(std::begin(_tour), std::end(_tour),
                                   [&visit](const auto& tour)
                                   {
                                       return tour.clientVisite == visit;
                                   }),
                    std::end(_tour));
        return original - _tour.size();
    }
    Problem *_Problem;
    vector<Tour> _tour;
    vector<unsigned int> length;
    unsigned int nbrCleint;
    unsigned int TotalDemande;
};

我还没有c++11

然后使用自定义函数对象:

struct equal_visit
{
    equal_visit(const std::vector<unsigned int>& l)
    : _l(l)
    {}
    bool operator()(const Tour& r) const
    {
        return _l == r.clientVisite;
    }
    const std::vector<unsigned int>& _l;
};
struct Solution {
    /// Remove every tour where the tour's visit array exactly matches visit
    /// @param visit is a vector of unsigned ints representsing locations
    /// @returns the number of tours removed
    std::size_t
    remove_tour_where_visit_matches(const std::vector<unsigned int>& visit)
    {
        std::size_t original = _tour.size();
        _tour.erase(std::remove_if(_tour.begin(), _tour.end(),
                                   equal_visit(visit)),
                    _tour.end());
        return original - _tour.size();
    }
    Problem *_Problem;
    vector<Tour> _tour;
    vector<unsigned int> length;
    unsigned int nbrCleint;
    unsigned int TotalDemande;
};