2016-09-28 108 views
-1

這篇文章是在我的崗位延續昨天:How to output a vector.C++擦除 - 刪除成語不能正常工作?

所以我在寫一個小程序,將採取字符串列表爲載體,將其導出到文件的過程中,在輸出之前查看它們他們最後刪除它們,如果可能的話。除了案例3(從列表中刪除一本書)之外,我設法使所有功能正常工作。我在Visual Studio中得到如下的錯誤:「參數列表匹配[LN 76]

2)‘

1)刪除’沒有重載函數的實例」「刪除':函數不需要2個參數「[LN 76]

正如你可能會告訴我,我試圖通過價值而不是索引去除,我仍然在這裏學習,所以要溫柔,但爲什麼我得到這些錯誤?

這是我的完整代碼:

#include <iostream> 
#include <cctype> 
#include <ctype.h> 
#include <algorithm> 
#include <string> 
#include <math.h> 
#include <windows.h> 
#include <iomanip> 
#include <vector> 
#include <iostream> 
#include <sstream> 
#include <fstream> 
#include <istream> 

// common namespace 
using namespace std; 

int main() 
{ 

    int option; 
    bool iWannaLeave = false; 

    vector<string> bookCollection; 
    string entryVal = " "; 
    int anotherOption; 
    do 
    { 
     cout << "Welcome to MyBook! - A text recorder framework application." << endl; 
     cout << "-----------------------------------------------------------" << endl; 
     cout << "Main Menu:" << endl; 
     cout << "1 - Add a book to the collection." << endl; 
     cout << "2 - Display all books currently in the collection." << endl; 
     cout << "3 - Remove books from the collection." << endl; 
     cout << "4 - Write stored collection to file." << endl; 
     cout << "5 - Quit" << endl; 
     cout << "Make your selection: "; 
     cin >> option; 
     cin.ignore(); 

     switch (option) 
     { 
     case 1: 
     { 
      bool wannaMoreBooks = false; 
      // the next loop will execute at least one time so you could enter a book 
      do 
      { 
       wannaMoreBooks = false; 
       cout << "Add a book title to the collection: "; 
       getline(cin, entryVal); 
       bookCollection.push_back(entryVal); 

       cout << "Would you like to enter another book?(1 - yes, 0 - no): "; 

       cin >> anotherOption; 
       cin.ignore(); 
       if (anotherOption == 1) wannaMoreBooks = true; 
      } while (wannaMoreBooks == true); 
     } 
     break; 

     case 2: 
     { 
      for (int i = 0; i < bookCollection.size(); i++) 
       cout << bookCollection[i] << " | "; 
      cout << endl; 
      break; 
     } 

     case 3: 
     { 
      string vecVal; 
      cout << "Enter the value you would like to remove: " << endl; 
      cin >> vecVal; 
      bookCollection.erase(remove(bookCollection.begin(), vecVal), bookCollection.end()); 
     } 
      // remove a book from the collection 
      break; 

     case 4: 
     { 
      ofstream fileOut("Collection.txt"); 
      fileOut << "Your MyBook Collection: [Begin] - | "; 
      auto first = true; 
      for (string x : bookCollection) 
      { 
       if (!first) { fileOut << " | "; } 
       first = false; 
       fileOut << x; 
      } 
      fileOut << " | - [End]" << endl; 
      cout << "Collection.txt has been successfully written." << endl; 
      break; 
     } 
     case 5: 
     { 
      //Nested IF to kill program properly 
      int quitVar; 
      cout << "Are you sure you want to exit the program?: "; 

      cin >> quitVar; 
      cin.ignore(); 

      if (quitVar == 1) 
      { 
       cout << "The program will now be terminated." << endl; 
       iWannaLeave = true; 
      } 
      else if (quitVar == 0) cout << "Returning to the main menu." << endl; 
     } 
     break; 
     } 
    } while (iWannaLeave == false); 

    return 0; 
} 

我知道,這是沒有在附近完美的代碼,所以除了找出爲什麼我收到這些錯誤我也想了一些建設性的批評,我怎麼能得到改善。

此外:如果我想在頭文件中使用函數而不是交換機,我只是將案例內容移動到頭文件?

在此先感謝! :)

+2

您沒有使用正確數量的參數。你應該仔細檢查[參考](http://en.cppreference.com/w/cpp/algorithm/remove)。 – NathanOliver

+2

如果您已經修復了代碼的功能問題,那麼您需要前往[codereview.se]查看您的風格評論。請記住閱讀那裏的介紹材料,以便能夠吸引最佳答案。 –

回答

0

幾乎所有STL函數都需要一個或多個對迭代器對。既然你只是通過begin,沒有可行的過載。你需要調用

remove(bookCollection.begin(), bookCollection.end(), vecVal) 

它始終是檢查reference,通常還包含了基本的使用示例是一個好主意。