无法通过.erase

时间:2018-01-15 19:17:15

标签: c++ vector erase

所以,我的问题是标题,雇用代码和编译器回复:

#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&gt; e:\ visualc \ survival_the_night \ survival_the_night \ survival_the_night.cpp(208):错误C3867:&#34; std :: vector&gt; :: begin&#34 ;:非标准语法;使用&#34;&amp;&#34;创建指向成员1的指针&gt;用1> [1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc \ survival_the_night \ survival_the_night \ survival_the_night.cpp(208):错误C2664:&#34; std :: _ Vector_iterator&gt;&gt; std :: vector&lt; _Ty,std :: allocator&lt; _Ty&gt;&gt; :: erase(std :: _ Vector_const_iterator&gt;&gt;,std :: _ Vector_const_iterator&gt;&gt;)&#34;:从&#34;转换参数1是不可能的。 int&#34; to&#34; std :: _ Vector_const_iterator&gt;&gt; &#34 1为卤素; 1>&gt;

     

[1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc \ survival_the_night \ survival_the_night \ survival_the_night.cpp(208):注意:没有构造函数可以接受源类型,或者构造函数重载权限不明确1&gt; e:\ visualc \ survival_the_night \ survival_the_night \ survival_the_night.cpp(211):错误C3867:&#34; std :: vector&gt; :: begin&#34 ;:非标准语法;使用&#34;&amp;&#34;创建指向成员1的指针&gt;用1> [1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc \ survival_the_night \ survival_the_night \ survival_the_night.cpp(211):错误C2664:&#34; std :: _ Vector_iterator&gt;&gt; std :: vector&lt; _Ty,std :: allocator&lt; _Ty&gt;&gt; :: erase(std :: _ Vector_const_iterator&gt;&gt;,std :: _ Vector_const_iterator&gt;&gt;)&#34;:从&#34;转换参数1是不可能的。 const char *&#34; to&#34; std :: _ Vector_const_iterator&gt;&gt; &#34 1为卤素;用1> [1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc \ survival_the_night \ survival_the_night \ survival_the_night.cpp(211):注意:无构造函数无法接受原始类型,或构造函数重载权限不是1:e:\ visualc ++ \ survival_the_night \ survival_the_night \ survival_the_night。 cpp(208):错误C3867:&#34; std :: vector&gt; :: begin&#34 ;:非标准语法;使用&#34;&amp;&#34;创建指向成员1的指针&gt;用1> [1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc ++ \ survival_the_night \ survival_the_night \ survival_the_night.cpp(208):错误C2664:&#34; std :: _ Vector_iterator&gt;&gt; std :: vector&lt; _Ty,std :: allocator&lt; _Ty&gt;&gt; :: erase(std :: _ Vector_const_iterator&gt;&gt;,std :: _ Vector_const_iterator&gt;&gt;)&#34;:无法从&#34;转换参数1 int&#34; to&#34; std :: _ Vector_const_iterator&gt;&gt; &#34 1为卤素; 1>&gt;

     

[1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc ++ \ survival_the_night \ survival_the_night \ survival_the_night.cpp(208):注意:没有构造函数可以接受原始类型,或构造函数重载权限不明确1&gt; e:\ visualc ++ \ survival_the_night \ survival_the_night \ survival_the_night.cpp(211):错误C3867:&#34; std :: vector&gt; :: begin&#34 ;:非标准语法;使用&#34;&amp;&#34;创建指向成员1的指针&gt;用1> [1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc ++ \ survival_the_night \ survival_the_night \ survival_the_night.cpp(211):错误C2664:&#34; std :: _ Vector_iterator&gt;&gt; std :: vector&lt; _Ty,std :: allocator&lt; _Ty&gt;&gt; :: erase(std :: _ Vector_const_iterator Ty&gt;&gt;&gt;,std :: enter code here Vector_const_iterator&gt;&gt;)&#34;:从&#34;转换参数1是不可能的。 const char *&#34; to&#34; std :: _ Vector_const_iterator&gt;&gt; &#34 1为卤素;用1> [1&GT; _Ty = std :: string 1&gt;] 1&gt; e:\ visualc ++ \ survival_the_night \ survival_the_night \ survival_the_night.cpp(211):注意:无构造函数无法接受原始类型,或构造函数重载权限不是永久性的`

很抱歉开始,Stacksoverflow只是不想理解,这也是代码。 是的,我知道这很糟糕,很糟糕的代码,对此感到抱歉。

2 个答案:

答案 0 :(得分:0)

您可以像这样处理这两种情况。

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

Ittems.erase(Ittems.begin +&#34; Little Pice of Meat&#34;);

我不知道您是要删除此条目,还是基本上删除了此条目的所有元素,但无论如何,我假设您要删除该元素。 使用:

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

答案 1 :(得分:-1)

在:     Ittems.erase(Ittems.begin

你没有调用begin,你需要一对(),但是向向量迭代器添加一个字符串文字也没有意义,所以重新考虑你的添加。

  • &#34; Little Pice of Meat&#34;);