使用索引从向量中删除对象

时间:2021-09-18 04:20:20

I have a class with a private member type with a getType in it, in a second class I have a vector of such class that I can add to as many classes as I want, now want I want to do is if I was given a "Type" I want to remove the whole Object from such vector by finding that object using that string and erase it. I have tried the way below but did not work, also tried iterators and templates yet none seem to work. * This is simplified for the sake of it*

我有一个带有getType的私有成员类的类,在第二个类中我有一个这样的类的向量,我可以添加到我想要的任意数量的类,现在我想要做的就是如果给了我一个“类型”我想通过使用该字符串查找该对象并将其删除来从此向量中删除整个对象。我尝试过以下方式,但没有工作,也尝试了迭代器和模板,但似乎没有工作。 *这是为了简化*

class AutoMobile{
    private:
      string type;
    public:
        AutoMobile(string type){
            this->type = type;
        }
        string getType(){return type;}
};


class Inventory{
    private:
        vector<AutoMobile> cars;
    public:
        void removeFromInventory(string type){    // No two cars will have the same milage, type and ext
            AutoMobile car("Ford");
            cars.push_back(car);
            for( AutoMobile x : cars){
                cout<<x.getType();
            }
            for( AutoMobile x : cars){
                if(x.getType() == "Ford"){
                    cars.erase(*x); // Problem i here, this does not work!
                 }
            }
        }
};

int main(void) {
    Inventory Inven;
    Inven.removeFromInventory("Ford");
   return 0;
}

2 个解决方案

#1


1  

Use of range for loop is not appropriate when you intend to remove items from a std::vector. Use an iterator instead.

当您打算从std :: vector中删除项目时,使用range for循环是不合适的。请改用迭代器。

vector<AutoMobile>::iterator iter = cars.begin();
for ( ; iter != cars.end(); /* Don't increment the iterator here */ )
{
   if ( iter->getType() == "Ford" )
   {
      iter = cars.erase(iter);
      // Don't increment the iterator.
   }
   else
   {
      // Increment the iterator.
      ++iter;
   }
}

You can simplify that block of code by using standard library functions and a lambda function.

您可以使用标准库函数和lambda函数来简化该代码块。

cars.erase(std::remove_if(cars.begin(),
                          cars.end(),
                          [](AutoMobile const& c){return c.getType() == 
"Ford";}),
                          cars.end());

#2


1  

You can use remove_if

你可以使用remove_if

cars.erase(std::remove_if(cars.begin(), 
                          cars.end(),
                          [=](AutoMobile &x){return x.getType()==type;}),
           cars.end());

#1


1  

Use of range for loop is not appropriate when you intend to remove items from a std::vector. Use an iterator instead.

当您打算从std :: vector中删除项目时,使用range for循环是不合适的。请改用迭代器。

vector<AutoMobile>::iterator iter = cars.begin();
for ( ; iter != cars.end(); /* Don't increment the iterator here */ )
{
   if ( iter->getType() == "Ford" )
   {
      iter = cars.erase(iter);
      // Don't increment the iterator.
   }
   else
   {
      // Increment the iterator.
      ++iter;
   }
}

You can simplify that block of code by using standard library functions and a lambda function.

您可以使用标准库函数和lambda函数来简化该代码块。

cars.erase(std::remove_if(cars.begin(),
                          cars.end(),
                          [](AutoMobile const& c){return c.getType() == 
"Ford";}),
                          cars.end());

#2


1  

You can use remove_if

你可以使用remove_if

cars.erase(std::remove_if(cars.begin(), 
                          cars.end(),
                          [=](AutoMobile &x){return x.getType()==type;}),
           cars.end());