2015-02-11 101 views
0

我想編寫一個字符串列表。用戶可以添加到列表中或從列表中刪除以及顯示當前列表。C++遍歷一個向量並刪除匹配的字符串

顯示列表和添加到列表工作正常,但我不知道如何通過遍歷列表中找到匹配來刪除用戶的字符串。

我該如何更改我的代碼來解決這個問題? 看,如果(答案== 3)

// InClassAssignment-FavouriteGameList.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <string> 
#include <cstdlib> 
#include <iostream> 
#include <vector> 
#include <algorithm> 
#include <ctime> 
#include <cctype> 

using namespace std; 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    vector <string> gameList; 
    int answer = 0; 
    bool cont = true; 
    int size = 0; 
    vector<string>::iterator iter; 
    string addToList; 
    string removeFromList; 

    cout << "\tGame List" << endl; 

    while (cont) 
    { 
     cout << "--------------------------------------------" << endl; 
     cout << "\nWhat do you want to do?"; 
     cout << "\n1 - Display List"; 
     cout << "\n2 - Add to List"; 
     cout << "\n3 - Remove from List"; 
     cout << "\n4 - End program" << endl << "Selection: "; 
     cin >> answer; 

     cout << endl; 

     if (answer == 1) 
     { 
      cout << "List: "; 
      for (iter = gameList.begin(); iter != gameList.end(); ++iter) 
      { 
       if (iter != gameList.end() - 1) 
        cout << *iter << ", "; 
       else 
        cout << *iter << endl; 
      } 
     } 

     else if (answer == 2) 
     { 
      cout << "Type in a game to add: "; 
      cin >> addToList; 
      gameList.push_back(addToList); 
      cout << "\nAdded (" << addToList << ") to your list." << endl; 
     } 

     else if (answer == 3) 
     { 
      //display list 
      cout << "List: "; 
      for (iter = gameList.begin(); iter != gameList.end(); ++iter) 
      { 
       if (iter != gameList.end() - 1) 
        cout << *iter << ", "; 
       else 
        cout << *iter << "\n" << endl; 
      } 

      //ask which one to remove 
      cout << "Which game should be removed?: "; 
      cin >> removeFromList; 

      //loop/iterate through the list to find a match and erase it 
      for (iter = gameList.begin(); iter != gameList.end(); ++iter) 
      { 
       if() 
        cout << "\nRemoved (" << removeFromList << ")" << endl; 
       else 
        cout << "\nGame not found" << endl; 
      } 

     } 

     else 
     { 
      cont = false; 
     } 

    } 

    cout << "\nThanks for using the program!\n" << endl; 

    return 0; 
} 

回答

5

您可以使用std::find獲得迭代器匹配要刪除的項目,然後調用vector::erase(iter)

auto iter = std::find(gameList.begin(), gameList.end(), removeFromList); 
if (iter != gameList.end()) 
{ 
    gameList.erase(iter); 
} 
+0

注意:您不需要if。擦除是有效的範圍[開始(),結束()] – 2015-02-11 20:31:51

+0

它的工作感謝,唯一的事情我很困惑它爲什麼使用自動而不是寫它呢? – Will 2015-02-11 20:35:47

+3

@DieterLücking實際上,對於'erase'的單一參數形式來說,檢查是必需的,因爲參數需要可解引用,而'end()'不需要。它在序列容器要求表中的某處提到。該要求也列在cppreference上(http://en.cppreference.com/w/cpp/container/vector/erase)。 – Praetorian 2015-02-11 20:39:36

1

看看在remove。它移動在序列結束時,所有匹配的元素,你便可以截斷它:

input.erase(
    remove(input.begin(), input.end(), s) 
    , input.end()); 

編輯:從弗雷德的意見納入建議。

+1

我更喜歡[erase-remove](https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom)成語。請參閱http://stackoverflow.com/q/22860119/10077 – 2015-02-11 20:59:57

+0

@FredLarson:對。謝謝。 – 2015-02-12 00:00:46