2016-04-01 5 views
0

私は別の構造体を含む構造体を持っており、最初の構造体から要素を削除したいと思います。 1 **最初の構造:他の構造体を含むstructurから要素を挿入して削除する

struct Solution { 

    Problem *_Problem; 

    vector<Tour> _tour; . 
    vector<unsigned int> length; 
    unsigned int nbrCleint; 
    unsigned int TotalDemande; 
} 

2 **第2の構造:二つの構造は、次のように定義されてい

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

そして今、私は「ソリューション」構造から要素を削除したいと思います。 exempleためsolution._tour.clientVisite == "いくつかのベクトル"

私はこれをどのように行うことができます要素を削除しますか?同じ原理で要素を挿入するにはどうすればよいですか?

注C++ < C++ 11

答えて

0
Solution s; 
for (vector<Tour>::iterator i = s._tour.begin(); i != s._tour.end(); ++i) { 
    if (i->clientVisite == somevector) { 
    s._tour.erase(i); break; 
    } 
} 
+0

あなたの答えはあなたですが、それはうまくいきません! –

+1

このコードで質問に答えるかもしれませんが、_why_および/または_how_に関する追加のコンテキストを に追加すると、 という質問に回答すると、その長期の 値が大幅に改善されます。あなたの答えを[編集]して、説明を加えてください。 –

0
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; 
}; 

私はまだ

次にカスタム関数オブジェクトを使用する++ 11 cの必要はありません。

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; 
}; 
+0

あなたの答えに感謝しますが、lambda式は-std = C++ 11でしか利用できません.C++ 11はありません。構造体にコードを追加すると多くの例外が発生します。 –

+1

lambaを等価関数オブジェクト。 –

+2

@Mouaici_Med質問に明記し、C++ 03タグを追加する必要があります。 – juanchopanza

関連する問題