trinathon
trinathon

Reputation: 99

Find a specific element within a vector

I have a class team that contains information for football teams. I need to read in a file and add each unique team to a vector season.

//Loop to determine unique teams

    if(season.size() <= 1)
        {
            season.push_back(new_team);
            cout << "First team added!" << endl;
        }

    vector<team>::iterator point;
    point = find(season.begin(), season.end(), new_team);
    bool unique_team = (point != season.end());
    if(unique_team == true && season.size()>1)
        {
            season.push_back(new_team);
            cout << "New team added!" << endl;
        }

    cout << "# of Teams: " << season.size() << endl;
    system("pause");

Any ideas why this doesn't work? I'm still new to this :-) So feel free to give constructive criticism.

Upvotes: 1

Views: 92

Answers (1)

jazzdawg
jazzdawg

Reputation: 564

I think your logic may be a little off. There first team should be added when the size of the teams vector is 0. Say your team is a vector of integers an insertTeam function would look something like this.

void Season::insertTeam(int team)
{
    if (teams.size() == 0)
    {
        teams.push_back(team);
        cout << "First team " << team << " added!" << endl;
    }
    else
    {
        vector<int>::iterator point;
        point = find(teams.begin(), teams.end(), team);
        bool unique_team = (point == teams.end());
        if(unique_team == true && teams.size()>0)
        {
            teams.push_back(team);
            cout << "New team " << team << " added!" << endl;
        }
    }
}

Upvotes: 1

Related Questions