无法通过 .erase 从 vector c++ 中删除任何内容

Cant erase anything from vector c++ by .erase

所以,我的问题全在title,hire是代码,编译器回复:

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

using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));
    double HP = 100; // Use It Somehow!
    double EP = 10; //
    double XP = 0; //
    double Strengh = 10; //
    const int Ittems_Max = 25;
    int Hour_1;
    int Hour_2;
    int Hour_3;
    int Hour_4;
    int Hour_5;
    int Hour_6;

    int Have_A_Tree = 0;
    int Have_A_Phone = 0;
    int Have_A_LPOM = 0;

    int N_F_D = 0;

    int random_number = (rand() % 10) + 1;

    int H_2_1 = 0;
    int H_2_2_1 = 0;
    int H_2_2_2 = 0;
    int H_2_2_3 = 0;
    int H_2_3 = 0;
    int H_2_L = 0; //L For Leter
    int H_3_1_1 = 0;
    int H_3_1_2 = 0;
    int H_3_1_3 = 0;
    int H_3_1_4 = 0;
    int H_3_1_O = 0; // O For Other (Number)
    int H_3_3 = 0;
    int H_3_Other = 0;

    vector<string> Ittems;
    Ittems.push_back("Axe");
    Ittems.push_back("Bootle With Water");
    Ittems.push_back("Matches");

    vector<string>::iterator Ittems_Iter;

    for (int i = 0; i < (Ittems).size(); ++i)
    {
        cout << Ittems[i] << endl;
    }

    cout << "This Is Your Ittems." << endl
            << "What Are You Want To Do In Forest, Alone?" << endl
            << "1.Try To Find Someone." << endl << "2.Try Cut Down A Tree."
            << endl << "3.Sit Down And Do Nothing." << endl;

    cin >> Hour_1;

    switch (Hour_1)
    {
        case 1:
            if (random_number > 5)
            {
                Ittems.push_back("Phone");
                Have_A_Phone = 1;
            }
            break;

        case 2:
            Ittems.push_back("Tree");
            Have_A_Tree = 1;
            break;
        case 3:
            Ittems.push_back("Little Pice Of Meat");
            Have_A_LPOM = 1;
            break;
        default:

            cout << "You Just Sit And Do Nothing." << endl;
            break;

    }

    if (Have_A_Phone == 1)
    {
        cout << "You Do Not Find Somebody But Find A Phone ." << endl;
    }

    if (Have_A_Tree == 1)
    {
        cout << "You Cut Out Tree." << endl;
    }

    if (Have_A_LPOM == 1)
    {
        cout
                << "You Saw A Little Squirrel, And Hunt It, Now You Hawe A Great Pice Of Meat."
                << endl;
    }

    Sleep(2000);
    system("CLS");

    cout << "This Is Your Invertory" << endl << endl;

    for (int i = 0; i < (Ittems).size(); ++i)
    {
        cout << Ittems[i] << endl;
    }

    Sleep(4000);
    system("CLS");

    cout << "\nWhat Do You Want To Do Now?(Left 5 Hours)\n" << endl
            << "1.Make Fire (Uses Tree And Matches).\n" << "2.Hunt.\n"
            << "3.Do Nothing.\n" << endl;

    cin >> Hour_2;

    switch (Hour_2)
    {

        case 1:
        if (Have_A_Tree == 1)
        {
            Ittems.pop_back();
            Ittems.pop_back();
            Have_A_Tree = 0;
            H_2_1 = 1;
        }
        else
        {
            cout << "You Hawe No Tree, And Just Do Nothing." << endl;
            H_2_3 = 1;
        }
        break;
        case 2:
        if (random_number < 5)
        {
            Ittems.push_back("Carrot");
            H_2_2_1 = 1;
        }
        if (random_number > 5)
        {
            Ittems.push_back("Rabbit Meat");
            H_2_2_2 = 1;
        }
        if (random_number == 5)
        {
            Ittems.push_back("Canned Stew");
            H_2_2_3 = 1;
        }
        break;
        case 3:
        if (true)
        {
            H_2_3 = 1;
        }
        break;

        default:
        H_2_L = 1; //L For Leter        break;  }

        cout << endl;

        for (int i = 0; i < (Ittems).size(); ++i)
        {
            cout << Ittems[i] << endl;
        }

        if (H_2_1 == 1)
        {
            cout << "Fire Is Hot." << endl;
        }

        if (H_2_2_1 == 1)
        {
            cout << "You Find A Carrot , But Not A Rabbit." << endl;
        }

        if (H_2_2_2 == 1)
        {
            cout << "So , You Find And Hunt Rabbit ,Congratulations!" << endl;
        }

        if (H_2_2_3 == 1)
        {
            cout << "WoW, This Thing Is Realy Rare, You Are Well." << endl;
        }

        if (H_2_3 == 1)
        {
            cout << "You Sit And Do Nothing, So, Nothing Hapend." << endl;
        }
        if (H_2_L == 1)
        {
            cout << "You Sit With \" " << Hour_2
                    << " \" , Maybe You Are Happy With It." << endl;
        }

        Sleep(4000);
        system("CLS");

        cout << "Woah, Third Hour Incoming, What Would You Do? \n"
                << "1.Try To Use Phone.\n" << "2.MakeA Dinner.\n"
                << "3.Do Nothing.\n" << endl;

        cin >> Hour_3;

        switch (Hour_3)
        {

            case 1:
                if (random_number == 4)
                {
                    H_3_1_1 = 1;
                }
                if (random_number == 9)
                {
                    H_3_1_2 = 1;
                }
                if (random_number == 6)
                {
                    H_3_1_3 = 1;
                }
                if (random_number == 3)
                {
                    H_3_1_4 = 1;
                }
                if (random_number == !4 && random_number == !9
                        && random_number == !6 && random_number == !3)
                {
                    H_3_1_O = 1; // Other - O       }       break;

                    case 2:
                    if (N_F_D = find(Ittems.begin(),
                                     Ittems.end(),
                                     "Rabbit Meat") = !Ittems.end)
                    {
                        Ittems.erase(Ittems.begin + N_F_D);
                    }
                    if (Have_A_LPOM == 1)
                    {
                        Ittems.erase(Ittems.begin + "Little Pice Of Meat");
                    }
                    break;
                    case 3:
                    H_3_3 = 1;
                    break;
                    default:
                    H_3_Other = 1;
                    break;
                }

                cout << endl;

                for (int i = 0; i < (Ittems).size(); ++i)
                {
                    cout << Ittems[i] << endl;
                }

                system("pause");

                return 0;
        }
`

现在编译器:

` 1> e: \ visualc \ survive_the_night \ survive_the_night \ survive_the_night.cpp (208): error C3867: "std :: vector > :: begin": non-standard syntax; use "&" to create a pointer to the member 1> with 1> [1> _Ty = std :: string 1>] 1> e: \ visualc \ survive_the_night \ survive_the_night \ survive_the_night.cpp (208): error C2664: "std :: _ Vector_iterator >> std :: vector <_Ty, std :: allocator <_Ty >> :: erase (std :: _ Vector_const_iterator >>, std :: _ Vector_const_iterator >>) ": it is impossible to convert argument 1 from" int "to" std :: _ Vector_const_iterator >> "1> with 1>>

[1> _Ty = std :: string 1>] 1> e: \ visualc \ survive_the_night \ survive_the_night \ survive_the_night.cpp (208): note: No constructor could accept the source type, or the constructor overload permission is ambiguous 1> e: \ visualc \ survive_the_night \ survive_the_night \ survive_the_night.cpp (211): error C3867: "std :: vector > :: begin": non-standard syntax; use "&" to create a pointer to the member 1> with 1> [1> _Ty = std :: string 1>] 1> e: \ visualc \ survive_the_night \ survive_the_night \ survive_the_night.cpp (211): error C2664: "std :: _ Vector_iterator >> std :: vector <_Ty, std :: allocator <_Ty >> :: erase (std :: _ Vector_const_iterator >>, std :: _ Vector_const_iterator >>) ": it is impossible to convert argument 1 from" const char * "to" std :: _ Vector_const_iterator >> "1> with 1> [1> _Ty = std :: string 1>] 1> e: \ visualc \ survive_the_night \ survive_the_night \ survive_the_night.cpp (211): note: None The constructor could not accept the original type, or the constructor overload permission was not 1: e: \ visualc ++ \ survive_the_night \ survive_the_night \ survive_the_night.cpp (208): error C3867: "std :: vector > :: begin": non-standard syntax; use "&" to create a pointer to the member 1> with 1> [1> _Ty = std :: string 1>] 1> e: \ visualc ++ \ survive_the_night \ survive_the_night \ survive_the_night.cpp (208): error C2664: "std :: _ Vector_iterator >> std :: vector <_Ty, std :: allocator <_Ty >> :: erase (std :: _ Vector_const_iterator >>, std :: _ Vector_const_iterator >>) ": it is not possible to convert argument 1 from" int "to" std :: _ Vector_const_iterator >> "1> with 1>

[1> _Ty = std :: string 1>] 1> e: \ visualc ++ \ survive_the_night \ survive_the_night \ survive_the_night.cpp (208): note: No constructor could accept the original type, or the constructor overload permission is ambiguous 1> e: \ visualc ++ \ survive_the_night \ survive_the_night \ survive_the_night.cpp (211): error C3867: "std :: vector > :: begin": non-standard syntax; use "&" to create a pointer to the member 1> with 1> [1> _Ty = std :: string 1>] 1> e: \ visualc ++ \ survive_the_night \ survive_the_night \ survive_the_night.cpp (211): error C2664: "std :: _ Vector_iterator >> std :: vector <_Ty, std :: allocator <_Ty >> :: erase (std :: _ Vector_const_iterator Ty >>>, std :: enter code here Vector_const_iterator >>) ": it is impossible to convert argument 1 from" const char * "to" std :: _ Vector_const_iterator >> "1> with 1> [1> _Ty = std :: string 1>] 1> e: \ visualc ++ \ survive_the_night \ survive_the_night \ survive_the_night.cpp (211): note: None The constructor could not accept the original type, or the constructor overload permission was not permanently `

一开始很抱歉,Stacksoverflow 只是不想理解,这也是代码。 是的,我知道这是非常糟糕的代码,很抱歉。

在: Ittems.erase(Ittems.begin

您不调用 begin,您需要一对 (),但将字符串文字添加到向量迭代器也没有任何意义,因此请重新考虑您的添加。

  • "Little Pice Of Meat");

你可以这样处理这两种情况。

#...
 #include <algorithm>
..# //include this library then

Ittems.erase(Ittems.begin + "Little Pice Of Meat");

我不知道你是想删除这个条目,还是基本上删除这个条目之前的所有元素,但无论如何,我假设你想删除这个元素。 使用:

auto dis = find(Ittems.begin(), Ittems.end(), "Little Pice Of Meat");
Ittems.erase(Ittems.begin() + std::distance(Ittems.begin(), dis));  //use 
       //distance to get the position of the element in the vector

或使用

Ittems.remove(Ittems.begin(), Ittems.end(), "Little Pice Of Meat");