从向量中删除元素

时间:2017-03-20 02:22:44

标签: c++ vector

我是C ++的新手,我正在编写一个程序来模拟一群兔子。该程序将能够添加它们,给它们名称,年龄,颜色等。现在我有一个工作程序,将在每次传递后添加兔子并将它们逐渐加1。我怎么能让程序删除一个兔子一旦兔子达到10岁。

代码:

enter code here
#include <iostream>
#include <ctime>
#include <vector>
#include <cstdlib>
#include <limits>

using namespace std;

const int  POSSIBLE_NAMES = 18;
const int  POSSIBLE_COLORS = 4;

static std::string possibleNames[] ={
    "Jen",
    "Alex",
    "Janice",
    "Tom",
    "Bob",
    "Cassie",
    "Louis",
    "Frank",
    "Bugs",
    "Daffy",
    "Mickey",
    "Minnie",
    "Pluto",
    "Venus",
    "Topanga",
    "Corey",
    "Francis",
    "London",
};
static std::string possibleColors[] ={

    "White",
    "Brown",
    "Black",
    "Spotted"
};

struct Bunny
{
public:

string name;
int age;
string color;
char sex;

Bunny(){
    setSex();
    setColor();
    setAge(0);
    setName();
}

int randomGeneration(int x){
    return rand() % x;
    srand (time(NULL));
}

void setSex()
{
    int randomNumber = randomGeneration(2);
    ( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
}

char getSex()
{
    return sex;
}

void setColor()
{
    int randomNumber = randomGeneration(POSSIBLE_COLORS);
    color = possibleColors[randomNumber];
}

string getColor()
{
    return color;
}

void setAge(int age)
{
    this->age = age;
}

int getAge()
{
    age++;
    return age;
}

void setName()
{
    int i = randomGeneration(POSSIBLE_NAMES);
    name = possibleNames[i];
}

string getName()
{
    return name;
}

void deleteBunny(){
    if (age > 10){
        cout << getName() << " has died" << endl;
    }
}
void printBunny()
{
    cout << "Name: " << getName() << endl;
    cout << "Sex: " << getSex() << endl;
    cout << "Color: " << getColor() << endl;
    cout << "Age: " << getAge() << endl;
}
};

int main()
{
   vector< Bunny > colony;
   char quit = '\0';
   do
   {
      // Add more bunny
      for (int i = 0; i < 5; i++)
      {
         colony.push_back(Bunny());
      }
      // Print all the bunny
      for (int i =0; i < colony.size(); i++)
      {
         colony[i].printBunny();
         colony[i].deleteBunny();
         cout << endl;
      }
      cout << "You have a total of " << colony.size() << " bunnies\n";
      cout << "Press a key to add more bunny, q to quit\n";
      quit = cin.get();
      cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
       // repeat
   } while (quit != 'q' && quit != 'Q');

   return 0;
}

2 个答案:

答案 0 :(得分:1)

您必须使用erase remove idiom

std::erase(colony.remove_if( colony.begin(), colony.end(),
                       [] (bunny& oldBunny){ return oldBunny.age > 10; } ), colony.end()); 

答案 1 :(得分:0)

您需要检查每个过程是否超过十,并从向量中删除它们。

vector< Bunny > colony;

   do
   {
      for(int i = 0; i < colony.size(); i++){
          if(colony[i].getAge() > 10) {
              // Call your delete function
              colony[i].deleteBunny();

              // Delete from vector
              colony.erase(colony.begin() + i);

              // Shift vector back down
              i--;
          }
      }
      // Add more bunny
      // Rest of code
  }