从向量中删除元素 我是C++新手,我正在编写一个程序,用来模拟一群兔子。该计划将能够添加他们,给他们的名字,年龄,颜色等,现在我有一个工作程序,将添加兔子后,每次通过和年龄他们1。我如何让程序在兔子10岁时删除它
守则:从向量中删除元素 我是C++新手,我正在编写一个程序,用来模拟一群兔子。该计划将能够添加他们,给他们的名字,年龄,颜色等,现在我有一个工作程序,将添加兔子后,每次通过和年龄他们1。我如何让程序在兔子10岁时删除它,c++,vector,C++,Vector,守则: 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 p
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;
}
您必须使用:
每次通过都需要检查是否超过10次,并将其从向量中删除
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
}
@如果把兔子从科隆尼亚移走,这是关于一类兔子的第三个问题:-
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
}