Search code examples
c++vectorerase

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


I am trying to use the vector erase function to delete a string that is an element of a vector for a homework assignment. I have tried the line two ways:

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

  2. vectnames.erase(blowup);

Does anyone know why the erase function might not be working? As a bit of background, I have to allow the user to enter a planet name to a vector and also let them delete it by name. The example I found online used line #2, but it's not working...

Here is the rest of my code for reference.

#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;
}

Solution

  • I'm going to work with vector<planet> planets; because it's got a nice ring to it and it's purpose is about as close to obvious as I can get without using an idiotically long name like VectorOfPlanets.

    In order to erase an item from a vector you have to know where it is. There are a bunch of ways to do this from brute force searching the vector in a loop until you find the index of the item you want and then calling planets.erase(planets.begin + index);

    I thought there was a overload of std::find you could pass a comparator function, but it looks like I'm on crack. Glad I didn't make an ass of myself by suggesting it. Wait... Am I writing out loud? Crap. Hate it when I do that.

    Read up on how to create an operator= method in the planet class and you can use std::find. With it you can:

    vector<planet> planets;
    vector<planet>::iterator it;
    
    it = std::find(planets.begin(), planets.end());
    if (it != planets.end())
    {
        planets.erase(it);
    }