Pętla crashuje program

0

Cześć, robię zadanie i napotkałem pewien problem. Mam taki kod:

 
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;



int main()
{
    int T, N, A, B, It, Nt, If, Nf;
    int index_for_1 = 0, index_against_1 = 0, index_for_2 = 0, index_against_2 = 0;
    string Empty_string;
    vector < int > vote_for_1;
    vector < int > vote_for_2;
    vector < int > vote_against_1;
    vector < int > vote_against_2;

    cin >> T;
    cin >> N >> A >> B;

    for(int a = 0; a < N; a++) {
        cin >> It >> Nt >> If >> Nf;
        if ((It == 1) && (If == 2)) {
            vote_for_1.push_back ( Nt );
            vote_against_1.push_back ( Nf );
            index_for_1++;
            index_against_1++;
        }

        else if ((It == 2) && (If == 1)) {
            vote_for_2.push_back ( Nt );
            vote_against_2.push_back ( Nf );
            index_for_2++;
            index_against_2++;
        }

}

   for (int a = 0; a < index_for_1; a++) {
            for (int b = 0; b < index_against_2; b++) {
                if (vote_for_1[a] == vote_against_2[b]) vote_for_1.erase ( vote_for_1.begin() + a );
            }
    }


    for ( int a = 0; a < index_for_2; a++) {
           for ( int b = 0; b < index_against_1; b++) {
                if (vote_for_2[a] == vote_against_1[b]) vote_for_2.erase ( vote_for_2.begin() + a);
            }
    }

    sort ( vote_for_1.begin(), vote_for_1.end() );
    sort ( vote_for_2.begin(), vote_for_2.end() );


    if (!vote_for_1.empty()){
        cout << endl << vote_for_1.size() << endl;
        for (int a = 0; a < vote_for_1.size(); a++) {

            cout << vote_for_1[a] << " ";
        }
    } else cout << 0 << Empty_string;

    if (!vote_for_2.empty()){
        cout << endl << vote_for_2.size() << endl;
        for (int a = 0; a < vote_for_2.size(); a++) {

            cout << vote_for_2[a] << " ";
        }
    } else cout << 0 << Empty_string;
}

Problem w tym, że dopóki nie ma części

 for ( int a = 0; a < index_for_2; a++) {
           for ( int b = 0; b < index_against_1; b++) {
                if (vote_for_2[a] == vote_against_1[b]) vote_for_2.erase ( vote_for_2.begin() + a);
            }
    }

Program działa dobrze, ale po wstawieniu w/w pętli crashuje się w trakcie jej wykonywania. Wydaje mi się, że nie ma błędów składniowych, a wszystkie zmienne w pętle się zgadzają. Czy ktoś ma jakiś pomysł?

3

Nie wgłębiałem się, ale wygląda na to, że nie wziąłeś pod uwagę faktu, że wielkość wektora się zmienia po usunięciu elementu (btw: czemu nie używasz metody size()?).

Użyj idiomu erase-remove w bezpieczny sposób (szerzej opisałem to tutaj):

template<typename Container, typename Predicate>
void erase_if(Container& c, Predicate p)
{
    using std::begin;
    using std::end;
    using std::remove_if;
 
    auto realEnd = end(c);
    auto removedIt = remove_if(begin(c), realEnd, p);
 
    c.erase(removedIt, realEnd);
}

Swoją drogą, patrząc na to co robisz, może interesujące będą dla Ciebie set_intersection/set_difference?

1 użytkowników online, w tym zalogowanych: 0, gości: 1