什么不能从向量中删除用户定义的值?

What can't I delete a value defined by the user from a vector?

我正在尝试使用矢量 erase 函数删除一个字符串,该字符串是家庭作业的矢量元素。我已经尝试了两种方式:

  1. vectnames.erase(vectnames[blowup]);

  2. vectnames.erase(blowup);

有谁知道为什么擦除功能可能不起作用?作为背景知识,我必须允许用户将行星名称输入矢量,并让他们按名称删除它。我在网上找到的示例使用了第 2 行,但它不起作用...

这是我的其余代码以供参考。

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

class planet
{
    private:
        string n;
        double d, m;
    public:
        void Density (double d, double m)
        {
            double Den = m/((4.0/3.0)*M_PI*pow((d/2.0), 3.0));
            cout<<"Density: "<<Den<<endl;
        }
        void SurfaceArea(double d)
        {
            double S = 4.0*M_PI*pow((d/2.0), 2.0);
            cout<<"Surface Area: "<<S<<endl;
        }   
        void Name (string n)
        {
            string N = n;
            cout<<"Name: "<<N<<endl;
        }
        void Gravity (double G, double m, double d)
        {
            double F = G*m/pow((d/2.0), 2.0);
            cout<<"Force of gravity: "<<F<<endl;
        }

};

int main()
{
    const double G=6.67384e-11;
    int c=0;
    string n, N, blowup;
    double d=0.0, m=0.0, Den=0.0, S=0.0, F=0.0;
    vector<string> vectnames;
    vector<double> vectdiam;
    vector<double> vectmass;

    do 
    {
        cout<<"1. Add a planet\n";
        cout<<"2. Delete planet\n";
        cout<<"3. Find planet (by name)\n";
        cout<<"4. List all planets\n";
        cout<<"5. Sort (alphabetical order)\n";
        cout<<"6. Quit\n";
        cout<<endl;
        cout<<"Please select an option from the menu above."<<endl;
        cin>>c;
        cout<<endl;

        if(c==1)
        {
            planet red;

            cout<<"Enter the planet's name: ";
            cin>>n;
            cout<<"Enter the planet's diameter: ";
            cin>>d;
            cout<<"Enter the planet's mass: ";
            cin>>m;
            cout<<endl;

            vectnames.push_back(n);
            vectdiam.push_back(d);
            vectmass.push_back(m);

            red.Name(n);
            red.Density(d, m);
            red.SurfaceArea(d/2.0);
            red.Gravity(G, m, d);
            cout<<endl;


        }
        else if (c==2)
        {           
            cout<<"Fire the Death Star's superlaser at: "<<endl;
            cin>>blowup;

            vectnames.erase(vectnames[blowup]); //This is the part that I'm having trouble with

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

        }

        else if (c==4)
        {
            for (int i=0; i<vectnames.size(); i++)
            {   
                planet red;
                cout<<"Planet name: "<<vectnames[i]<<endl;
                cout<<"Planet diameter: "<<vectdiam[i]<<endl;
                cout<<"Planet mass: "<<vectmass[i]<<endl;
                red.Density(vectdiam[i], vectmass[i]);
                red.SurfaceArea(vectdiam[i]/2.0);
                red.Gravity(G, vectmass[i], vectdiam[i]);
                cout<<"************************"<<endl;
                cout<<endl;
            }
        }


    } while (c!=6);

    system("pause");

    return 0;
}

我将与 vector<planet> planets; 一起工作,因为它有一个很好的环,而且它的目的几乎是显而易见的,而无需使用像 VectorOfPlanets 这样的愚蠢的长名称.

为了从向量中删除一个项目,你必须知道它在哪里。有很多方法可以做到这一点,从暴力搜索循环中的向量,直到找到所需项目的索引,然后调用 planets.erase(planets.begin + index);

我以为有 std::find 的重载,您可以传递一个比较器函数,但看起来我要破解了。很高兴我没有通过建议让自己出洋相。等等……我在大声写吗?废话。讨厌我那样做。

阅读有关如何在星球 class 中创建 operator= 方法的信息,您可以使用 std::find。有了它,您可以:

vector<planet> planets;
vector<planet>::iterator it;

it = std::find(planets.begin(), planets.end());
if (it != planets.end())
{
    planets.erase(it);
}