Vector

Good morning guys!

Good Morning, guys!

Soo I have simple game in which I create bunnies. For each female bunny theres a male bunny born. They age one year each round and once they reach year 10, they should die, however it does with someone but doesnt with others. And there are bunnies with age 10 and bigger however, they do die eventually but later once they reach 13 17 or even 20.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
main.cpp

int main()
{

	ofstream output;
	output.open("bunny.txt");

	srand(static_cast<unsigned int>(time(0))); // set initial seed value to system clock
	rand(); // If using Visual Studio, discard first random value
	
	std::vector<Bunny> package;
	package.reserve(2000); // pre-allocate memory in one go
	
	
	// We create a starting 5 bunnies
	for (int i = 0; i < 5; i++)
	{
		// There is a 2% chance that a radioactive one will be created, so lets look if its true this time

		if (getRandomNumberObjectless(0, 49) == 1)
		{
			Bunny bunny = BunnyGenerator::generateRadioActiveBunny();
			output << "*********   " << bunny.getName() << " was created " << " ***************" << endl;
			package.push_back(bunny);
		}

		// If its not the radioactive one then we create a normal one
		else
		{
			Bunny bunny = BunnyGenerator::generateBunny();
			output << "Bunny " << bunny.getName() << " of color " << bunny.getColor() << " and a sex of " << bunny.getSex() << " was born!" << endl;
			package.push_back(bunny);
		}
	}
	output << string(3, '\n');
	//The whole logic of the game
	while (package.size() != 0)
	{
		if (package.size() > 999)
		{
			for (unsigned int i = 0; i < package.size(); i++)
			{
				int y = getRandomNumberObjectless(0, package.size() - 1);
				package.erase(package.begin() + y);
				package.shrink_to_fit();
			}

			
		}

		sort(package.begin(), package.end(), more_than_key());
			// Increase bunnies age
		for (unsigned int i = 0; i < package.size(); i++)
		{
			package[i].increaseAge();
			if (package[i].getAge() > 9 && (package[i].getSex() == "Female" || package[i].getSex() == "Male"))
			{
				output << "Bunny " << package[i].getName() << " sex " << package[i].getSex() << package[i].getColor() <<  " died!" << endl;
				package.erase(package.begin() + i);
				/*package.shrink_to_fit();*/
			}
			else if (package[i].getSex() == "NONE" && package[i].getAge() > 49)
			{
				output << "RadioActive Buny " << package[i].getName() << " died!" << endl;
				package.erase(package.begin() + i);
				/*package.shrink_to_fit();*/
			}

			package.shrink_to_fit();
		}
		

		/*Sleep(1000);*/
		output << string(3, '\n');

		// Create as many new rabits as there are females atleast year 2
		
		int count = 0;
		for (unsigned int i = 0; i < package.size(); i++)
		{

			// We check to see if there's a radioactive rabbit, if thats true we count it 
			if (package[i].getSex() == "NONE")
			{
				count++;
			}
		}
		for (int i = 0; i < count; i++)
		{
			int x = getRandomNumberObjectless(0, package.size() - 1);
			package[x] = BunnyGenerator::generateRadioActiveBunny();

		}
		
		

		for (unsigned int i = 0; i < package.size(); i++)
		{
			if (package[i].getSex() == "Female" && package[i].getAge() > 1)
			{

				//We check to see if the farm has reached an amount of 1000 habitants if so, we kill 500 of them.
				if (package.size() > 999)
				{
					for (unsigned int i = 0; i < 500; i++)
					{
						int y = getRandomNumberObjectless(0, package.size() - 1);
						package.erase(package.begin() + y);
						package.shrink_to_fit();
					}

					
				}
				

				// If the farm hasnt reached 1000 habitants radioactive rabbit may be created
				else if (getRandomNumberObjectless(0, 49) == 1)
				{
					Bunny bunny = BunnyGenerator::generateRadioActiveBunny();
					output << "*********   " << bunny.getName() << " was created " << " ***************" << endl;
					package.push_back(bunny);
				}
				// if radio active bunny wasnt created we create a normal bunny
				else
				{
					Bunny bunny = BunnyGenerator::generateBunny();
					bunny.m_color = package[i].m_color;
					output << "Bunny " << bunny.getName() << " of color " << bunny.getColor() << " and a sex of " << bunny.getSex() << " was born!" << endl;
					package.push_back(bunny);
				}
			}

		}
		output << string(3, '\n');
		/*Sleep(1000);*/
		

		sort(package.begin(), package.end(), more_than_key());
		// Print out all the faaarm
		for (vector<Bunny>::iterator it = package.begin(); it != package.end(); it++)
			output << *it << endl;
		
			
		output << string(3, '\n');
		/*Sleep(1000);*/
	
		
	}

	


	return 0;
}


I tried sorting in both places in one place and without sorting the problem still stayed.


This is Bunny.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#pragma once
#include <string>
class Bunny 
{
public:
	enum BunnyName
	{
		TOM,
		ANTHONY,
		JAMES,
		LEBRON,
		PIKE,
		SPOKE,
		MAX_NAMES,
		DARK,
		DARKNESS,
		LORDKILLER,
		OBSESSOR,
		MAX_RADIOACTIVE
	};
	
	enum BunnyColor
	{
		BROWN,
		BLACK,
		GREY,
		WHITE,
		SPOTTED,
		MAX_COLORS
	};


public:
	BunnyColor m_color;
protected:
	BunnyName m_name;
	
	std::string m_tester;
	std::string m_sex;
	int m_age;
	
public:
	Bunny(std::string tester = "CIUVAK", BunnyName name = MAX_RADIOACTIVE, BunnyColor color = MAX_COLORS,
		std::string sex = "OPA", int age = 1);
	Bunny(const Bunny &bunny);
	~Bunny();

	friend std::ostream& operator<<(std::ostream &out, Bunny &bunnny);
	Bunny& operator=(const Bunny &bunny);
	std::string getName();
	std::string getColor();
	std::string getSex() { return m_sex; }
	std::string getTester() { return m_tester; }
	int getAge() const { return m_age; }
	int increaseAge() { return m_age++; }
};


Could you try and take a look maybe im missing something so obvious that causes a problem?
And this is an output:

Bunny Lebron of color Black and a sex of Male was born!
Bunny Lebron of color Spotted and a sex of Male was born!
Bunny Spoke of color White and a sex of Male was born!
Bunny Lebron of color Brown and a sex of Male was born!
Bunny Tom of color White and a sex of Female was born!






Bunny James of color White and a sex of Male was born!



Lebron Black Male 2
Lebron Spotted Male 2
Spoke White Male 2
Lebron Brown Male 2
Tom White Female 2
James White Male 1






Bunny Spoke of color White and a sex of Male was born!



Lebron Black Male 3
Lebron Spotted Male 3
Spoke White Male 3
Lebron Brown Male 3
Tom White Female 3
James White Male 2
Spoke White Male 1






Bunny Spoke of color White and a sex of Male was born!



Lebron Black Male 4
Lebron Spotted Male 4
Spoke White Male 4
Lebron Brown Male 4
Tom White Female 4
James White Male 3
Spoke White Male 2
Spoke White Male 1






Bunny Tom of color White and a sex of Male was born!



Lebron Black Male 5
Lebron Spotted Male 5
Spoke White Male 5
Lebron Brown Male 5
Tom White Female 5
James White Male 4
Spoke White Male 3
Spoke White Male 2
Tom White Male 1






Bunny James of color White and a sex of Male was born!



Lebron Black Male 6
Lebron Spotted Male 6
Spoke White Male 6
Lebron Brown Male 6
Tom White Female 6
James White Male 5
Spoke White Male 4
Spoke White Male 3
Tom White Male 2
James White Male 1






Bunny Anthony of color White and a sex of Male was born!



Lebron Black Male 7
Lebron Spotted Male 7
Spoke White Male 7
Lebron Brown Male 7
Tom White Female 7
James White Male 6
Spoke White Male 5
Spoke White Male 4
Tom White Male 3
James White Male 2
Anthony White Male 1






Bunny Anthony of color White and a sex of Male was born!



Lebron Black Male 8
Lebron Spotted Male 8
Spoke White Male 8
Lebron Brown Male 8
Tom White Female 8
James White Male 7
Spoke White Male 6
Spoke White Male 5
Tom White Male 4
James White Male 3
Anthony White Male 2
Anthony White Male 1






Bunny Lebron of color White and a sex of Female was born!



Lebron Black Male 9
Lebron Spotted Male 9
Spoke White Male 9
Lebron Brown Male 9
Tom White Female 9
James White Male 8
Spoke White Male 7
Spoke White Male 6
Tom White Male 5
James White Male 4
Anthony White Male 3
Anthony White Male 2
Lebron White Female 1



Bunny Lebron sex MaleBlack died!
Bunny Spoke sex MaleWhite died!
Bunny Tom sex FemaleWhite died!
Bunny Spoke sex MaleWhite died!
Bunny Tom sex MaleWhite died!



Bunny Pike of color White and a sex of Male was born!



Lebron Spotted Male 10
Lebron Brown Male 10
James White Male 10
Spoke White Male 10
James White Male 10
Anthony White Male 9
Anthony White Male 8
Lebron White Female 7
Pike White Male 1



Bunny Lebron sex MaleSpotted died!
Bunny James sex MaleWhite died!
Bunny James sex MaleWhite died!
Bunny Anthony sex MaleWhite died!
Bunny Pike sex MaleWhite died!



Bunny Lebron of color White and a sex of Female was born!



Lebron Brown Male 11
Spoke White Male 11
Anthony White Male 11
Lebron White Female 11
Lebron White Female 1



Bunny Lebron sex MaleBrown died!
Bunny Anthony sex MaleWhite died!
Bunny Lebron sex FemaleWhite died!



Bunny Pike of color White and a sex of Female was born!



Spoke White Male 12
Lebron White Female 12
Pike White Female 1



Bunny Spoke sex MaleWhite died!
Bunny Pike sex FemaleWhite died!



Bunny Anthony of color White and a sex of Male was born!



Lebron White Female 13
Anthony White Male 1



Bunny Lebron sex FemaleWhite died!






Anthony White Male 14



Bunny Anthony sex MaleWhite died!









I wrote in your earlier thread: http://www.cplusplus.com/forum/general/203771/#msg967962

You have essentially:
1
2
3
4
5
for (unsigned int i = 0; i < package.size(); i++)
{
  age bunny i
  kill bunny i
}

Test case:

On first iteration the i==0
The first bunny is too old and must go.
You erase the first element of the vector. As result, the second bunny is at pos 0 now.

On second iteration the i==1
You are now looking at the third bunny. Your loop never touches the second bunny (but then it should not age either).
Last edited on
Hey, keskiverto, but now I cannot reach public member functions since the iterator is undefined i
1
2
3
4
5
6
7
8
9
for (auto &bunny : package) bunny.increaseAge();
		{
			if (bunny.getAge() > 9 && (bunny.getSex() == "Female" || bunny.getSex() == "Male"))
			{
				output << "Bunny " << bunny.getName() << " sex " << bunny.getSex() << bunny.getColor() << " died!" << endl;
				// cull the old: with remove-erase idiom and lambda function
				package.erase(std::remove_if(package.begin(), package.end(),
					[](auto b) { return 10 <= b.getAge(); }),
					package.end());


If i do it like this. Am I missing something, feel so stupid right now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// range-based for loop syntax:
for ( auto & bunny : package ) bunny.increaseAge();

// cull the old: with remove-erase idiom and lambda function
package.erase( std::remove_if( package.begin(), package.end(),
                               []( auto b ) { return 10 <= b.getAge() &&
                                              (b.getSex() == "Female" || b.getSex() == "Male"); } ),
               package.end() );

// cull the shiny
package.erase( std::remove_if( package.begin(), package.end(),
                               []( auto b ) { return 50 <= b.getAge() &&
                                              b.getSex() == "NONE"; } ),
               package.end() );


You could have the death cries of the bunnies in their very own destructor:
1
2
3
4
Bunny::~Bunny() {
  if ( "NONE" == m_sex ) std::cout << "RadioBunny " << m_name << " died!\n";
  else std::cout << "Bunny " << m_name << " sex " << m_sex << getColor() << " died!\n";
}

... or within the lambda:
[]( auto b ) { return 50 <= b.getAge() && b.getSex() == "NONE"; }
could be
1
2
3
[]( auto b ) { bool res = 50 <= b.getAge() && b.getSex() == "NONE";
  if ( res ) output << "RadioBunny " << b.getName() << " died!\n";
  return res; }


Hey,man, I don't think it's working. It still does miss a lot of them and i have no idea why it is how it is. Or actually I should say it increases other's more than it should and in a result we get other objects with bigger age. Hmm that is strange


EDIT:

I have an idea. Could it be the fact that every single element in the vector has to match the criteria to be removed?
Last edited on
Topic archived. No new replies allowed.