C++ C+中的人口计划+;
我急需帮助。 我正在为我的系统软件类C++编写人口计划。 这是我第一次进入C++领域,我只有一些java知识来帮助我。 基本上,该程序应该模拟一个简单的总体。指引如下:C++ C+中的人口计划+;,c++,C++,我急需帮助。 我正在为我的系统软件类C++编写人口计划。 这是我第一次进入C++领域,我只有一些java知识来帮助我。 基本上,该程序应该模拟一个简单的总体。指引如下: 第一个要素(起始人口)具有随机的年龄和性别 如果两个元素的寿命介于[0.25,0.50]之间(假设它们在1岁时死亡),并且它们是异性,那么它们可以配对 每个元素只能配对两次 P>这是我的代码,大家都放心,我对C++还不是很精通……: #include <vector> #include <ctime>
#include <vector>
#include <ctime>
#include <stdlib.h>
#include <iostream>
#include <iterator>
using namespace std;
class Element {
public:
int pair;
double life;
int tag;
void setValues(double, int);
int getPair() {
return pair;
}
void incrementPair() {
pair = pair++;
}
double getLife() {
return life;
}
void incrementLife() {
life = life + 0.05;
}
int getTag() {
return tag;
}
}; //Element
void Element::setValues(double b, int c) {
pair = 0;
life = b;
tag = c;
}
int main() {
double Time = 0.0;
srand(time(NULL));
vector<Element> vec;
for (int i = 0; i<50; ++i) {
Element x;
x.setValues(((double) rand() / (RAND_MAX)), rand()%2);
vec.push_back(x);
}//for
while (vec.size() != 0) {
int newPopCount = 0;
int Dead = 0;
for(int count = 0; count != vec.size(); ) {
std::vector<Element>::iterator p = vec.begin();
std::vector<Element>::iterator i = vec.begin() + 1;
if ((p->getPair() == 2) || (p->getLife() < 0.25) || (p->getLife() > 0.50)) {
count++;
p++;
}//if
else {
for(int count1 = count + 1 ; count1 != vec.size() ; ) {
if ((i->getLife() < 0.25) || (i->getLife() > 0.50) || (i->getPair() == 2) || (p->getTag() == i->getTag())) {
++i;
count1++;
}//if
else {
cout << i->getTag() << " " << p->getTag() << endl;
cout << i->getPair() << " " << p->getPair() << endl;
cout << i->getLife() << " " << p->getLife() << endl;
p->incrementPair();
i->incrementPair();
newPopCount++;
count1++;
count++;
p++;
i++;
}//else
}//for
}//else
}//for
Time += 0.05;
for ( vector<Element>::iterator m = vec.begin(); m != vec.end(); ++m ) {
m->incrementLife();
if ( m->getLife() >= 1.00 ) {
Dead++;
//vec.clear();
vec.erase(m);
//vec.shrink_to_fit();
}//if
for (int i = 0; i = newPopCount; i++) {
Element woo;
woo.setValues(0.0, rand()%2);
vec.push_back(woo);
}//for
}//for
cout << "The total number of dead is: " << Dead << endl;
cout << "The total number born is: " << newPopCount << endl;
cout << "Current population is: " << vec.size() << endl;
cout << "Population has survived for: " << Time << endl;
sleep(1);
}//while
cout<< "The populace is now extinct." << endl;
cout<< "The populace survived for: " << Time << endl;
}//main
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类元素{
公众:
整数对;
双重生活;
int标签;
无效设置值(双精度,整数);
int getPair(){
返回对;
}
void incrementPair(){
pair=pair++;
}
double getLife(){
回归生活;
}
void incrementLife(){
寿命=寿命+0.05;
}
int getTag(){
返回标签;
}
}; //元素
void元素::setValues(双b,int c){
配对=0;
寿命=b;
tag=c;
}
int main(){
双倍时间=0.0;
srand(时间(空));
向量向量机;
对于(inti=0;igetPair()=2)| |(p->getLife()<0.25)| |(p->getLife()>0.50)){
计数++;
p++;
}//如果
否则{
for(int count1=count+1;count1!=vec.size();){
如果((i->getLife()<0.25)| |(i->getLife()>0.50)| |(i->getPair()=2)| |(p->getTag()=i->getTag()){
++一,;
count1++;
}//如果
否则{
cout getTag()vec.erase(m);
会导致无效的m
。您要执行m=vec.erase(m)
两个明显的问题,如果您将警告调得足够高,编译器将警告您
第一:
void incrementPair() {
pair = pair++;
}
这是未定义的行为。应该是:
void incrementPair() {
++pair;
}
第二:
for (int i = 0; i = newPopCount; i++) {
Element woo;
woo.setValues(0.0, rand()%2);
vec.push_back(woo);
}
您的for
循环中的条件几乎肯定是错误的。它可能应该是i}//对于
——请不要。请正确缩进右大括号。对缩进做些什么迭代正在修改的数组是一个基本错误。在这种情况下,在迭代过程中更改了vec
。如果您正在查看第四个元素并决定删除它,是否希望转到“旧”第五个元素(现在是第四个),或“新的”第五个元素(是第六个)@ScottyMorris Identitation不是为了美观,而是为了便于理解代码,以便更轻松地修复它!使代码看起来美观可能有助于避免重大问题。如果你看不懂代码,你就不知道它在做什么。啊,这些似乎是我的疏忽。我感谢你的帮助。我的d+1,但我的声誉不太好够高了。哇,在实施了你提到的修复之后,我收到了一条疯狂的错误消息,内容是“无效指针”,但是我的控制台继续打印大量内存地址。这非常有用,我失去了很多“编程动力”在这一点上,我希望这能让我重新开始。如果你想得到一些建议,我建议把事情分解成更多的函数。即使是像If((I->getLife()<0.25)| |(I->getLife()>0.50)| |(I->getPair()=2)| | |(p->getag()=I->getTag
,将它们作为一个函数,并给它们一个有意义的名称,这将使您的逻辑更容易理解,也使您的程序更容易编写。例如,在这种特定情况下,类似于的内容(is_reproductive_age(*i)| is_paired(*i,*p)){/*…
。
for(int count = 0; count != vec.size(); ) {
std::vector<Element>::iterator p = vec.begin();
std::vector<Element>::iterator i = vec.begin() + 1;