2011-11-04 145 views
1

我有一個包含足球隊信息的班級團隊。我需要閱讀一個文件,並將每個獨特的團隊添加到矢量季節。查找向量中的特定元素

//循環來確定唯一的球隊

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"); 

任何想法,爲什麼這不起作用?我仍然對此感到陌生:-)因此,請隨時給予建設性的批評。

+0

我覺得你的邏輯可能有點過,不應該一隊加入當賽季矢量的大小爲0。如果一線隊已經加入你並不需要檢查它是否在矢量中再次添加它。您的查找(...)代碼正常工作。 – jazzdawg

回答

1

我認爲你的邏輯可能有點偏離。當團隊向量的大小爲0時,應該添加第一個團隊。假設你的團隊是一個整數向量,insertTeam函數看起來像這樣。

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