C ++ Erase-Remove Idiom无法正常工作?

时间:2016-09-28 14:26:46

标签: c++

此帖子是我昨天发布的帖子的延续:How to output a vector.

所以我正在编写一个简短的程序,将一个字符串列表转换为一个向量,将它们导出到一个文件,在输出它们之前查看它们,如果可能的话最后删除它们。除了案例3(从列表中删除一本书)之外,我已设法使所有功能正常工作。我在visual studio中遇到的错误如下:

1。)“没有重载函数的实例”remove“匹配参数列表。[LN 76]

2。)“'删除':函数不带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;
}

我知道这不是接近完美代码的地方所以除了找出我为什么会遇到这些错误之外,我还想对我如何改进进行一些建设性的批评。

另外:如果我想在头文件而不是交换机中使用函数,我是否只需将案例内容移动到头文件中?

提前致谢! :)

1 个答案:

答案 0 :(得分:0)

实际上所有STL函数都需要一个或多个迭代器。由于您只是通过begin,因此没有可行的过载。你需要打电话

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

检查reference始终是个好主意,{{3}}通常还包含一个基本用法示例。