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

时间:2015-05-09 05:10:38

标签: c++ vector erase

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

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

  2. vectnames.erase(blowup);

  3. 有谁知道为什么擦除功能可能无效?作为一些背景知识,我必须允许用户在矢量中输入行星名称,并让它们按名称删除它。我在网上找到的例子使用的是第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;
    }
    

1 个答案:

答案 0 :(得分:0)

我将与vector<planet> planets;一起工作,因为它有一个很好的戒指,而且它的目的几乎与我在没有使用白痴的情况下接近显而易见长名称如VectorOfPlanets

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

我认为std::find有一个超载你可以通过一个比较器功能,但看起来好像我在破解。很高兴我没有建议它自己做一个屁股。等等......我大声写字了吗?废话。我这样做时讨厌它。

了解如何在行星类中创建operator=方法,然后使用std::find。有了它,你可以:

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

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