Removing multiple elements from a vector c++ - c++

Here is some code which checks if 2 units are killed after they attack each other, I pass in the position in the vector however when I remove one the vector is changed in size and therefore the 2nd unit is out of range. How can I remove both simultaneously?
if ((MyHealth <= 0) && (EnemyHealth <= 0))
{
PlayerUnits.erase(PlayerUnits.begin() + MyUnit, PlayerUnits.begin() + EnemyUnit);
}
else if (MyHealth <= 0)
{
PlayerUnits.erase(PlayerUnits.begin() + MyUnit);
}
else if (EnemyHealth <= 0)
{
PlayerUnits.erase(PlayerUnits.begin() + EnemyUnit);
}

Instead of coding the removal logic yourself, it would be better managed using std::remove_if from algorithm. Depending on whether you have compiler supporting C++11 or not, the Predicate can either be a lambda or a named function.

First point: In your first block, the call erase(x, y) does something different than you expect - it erases a whole range of elements starting at index x until just before index y. For example, if we have the vector [a,b,c,d,e,f,g], then erase(2,5) will erase indexes 2,3,4, so we end up with [a,b,f,g]. I'm guessing you want to erase just two elements, not an entire range.
Second point: As Dieter Lücking pointed out, simply erase the higher index element first, like this:
if (MyUnit > EnemyUnit) {
PlayerUnits.erase(PlayerUnits.begin() + MyUnit);
PlayerUnits.erase(PlayerUnits.begin() + EnemyUnit);
} else {
PlayerUnits.erase(PlayerUnits.begin() + EnemyUnit);
PlayerUnits.erase(PlayerUnits.begin() + MyUnit);
}

I think a better way to handle this would be to add an "isDead" condition to your "unit" class:
void Unit::Update()
{
//other stuff
if(this->m_health <= 0) this->m_isDead = true;
}
then in the main loop:
void Game::Update()
{
size_t size = PlayerUnits.size();
//iterate backwards, so there is no skipping
for(int i = size-1; i>= 0; i--)
{
PlayerUnits[i]->Update();
if(PlayerUnits[i]->isDead()) PlayerUnits.erase(PlayerUnits.begin() + i);
}
}
This is at least how I personally do it.

Related

Simple recursive function to determine if two elements are transitively true

newbie here. Even newer to recursion. I'm writing a function for my C++ program, and as you'll be able to tell, I'm a bit clueless when it comes to recursive algorithms. I'd appreciate it greatly if someone could fix my function so I can get it working and perhaps have a better idea how to handle recursion afterward.
My function takes a two-dimensional square array of booleans, and integer i, and an integer array_size as parameters. The function returns a boolean value.
The array is an adjacency matrix that I use to represent a set of conditionals. For example, if the value at [0][3] is true, then 0 -> 3 (if 0, then 3). If [3][7] is true, then 3 -> 7 (if 3, then 7). By the transitive property, 0 -> 7 (if 0, then 7).
The integer i is a particular element in the set of conditionals. The function will return true if this element is transitively connected to the last element in the array. The last element in the array is the integer (array_size - 1),
The integer array_size is the size of each dimension of the square array. If array_size is 20, then the array is 20x20.
The idea of this function is to determine if there is any logical "path" from the first integer element to the last integer element by the transitive property. When the path exists, the function returns true, otherwise, it returns false. The recursive call should allow it to traverse all possible paths, returning true once it finally reaches the last element and false if all paths fail.
For example, if i = 0 and array_size = 10, then the function will return whether or not 0 -> 9 is valid according to the conditionals provided by the matrix and the transitive property.
This is my code so far:
bool checkTransitivity(bool **relations, int i, int array_size){
bool isTransitive = false;
if (i == array_size - 1)
{
isTransitive = true;
}
else
{
for (int j = i; j < array_size; j++){
if (relations[i][j])
{
isTransitive = checkTransitivity(relations, j, array_size);
}
}
}
return isTransitive;
Currently, the function returns true for all input.
Any help at all is appreciated. Thanks in advance!
EDIT: This first part is unnecessary because of your if-else statement. Move on to END OF EDIT.
Let's start with what a base case in a recursive function is:
if (i == array_size - 1)
{
isTransitive = true;
}
Well you do have a base case, but nothing is being returned. You are just setting a flag to true. What you want to do is:
if (i == array_size - 1) {
return true;
}
Now the function will work its way up the recursive stack to return true. END OF EDIT.
But we still need to fix the recursive case:
else {
for (int j = i; j < array_size; j++) {
if (relations[i][j]) {
isTransitive = isTransitive || checkTransitivity(relations, j, array_size);
}
}
}
return isTransitive;
The || means binary OR. So you have the logic right. You want to check each possible path to see if it can get there, but by setting isTransitive to the result of each check, isTransitive is only going to be set to the last call. By doing isTransitive = isTransitive || recursive call, isTransitive will be true as long as one of the calls results in a true value.
The last thing I want to say is a caution: if relations[i][j] == true and relations[j][i] == true, your code will still be in an infinite loop. You must find a way to eliminate the potential backtracking. One way to do this is to create another array that stores which paths you have already checked so you do not infinitely loop.
More information can be found here: Depth First Search
I think all you need is a break condition to stop continuing the loop when you encounter a non-transitive item. See below (haven't tested)
bool checkTransitivity(bool **relations, int i, int array_size){
bool isTransitive = false;
if (i == array_size - 1)
{
isTransitive = true;
}
else
{
for (int j = i; j < array_size; j++){
isTransitive = relations[i][j] && checkTransitivity(relations, j, array_size);
if (!isTransitive)
break;
}
}
return isTransitive;
}

How to determine the number of array 100 are not equal to each other

I am coding a Sudoku program. I found the number in the array determine whether duplicate each other is hard.
Now I have an array: int streamNum[SIZE]
if SIZE=3,I can handle this problem like:if(streamNum[0]!=streamNum[1])...
if SIZE=100,I think that I need a better solution, is there any standard practice?
There are a couple of different ways to do this, I suppose the easiest is to write two loops
bool has_duplicate = false;
for (int i = 0; i < SIZE && !has_duplicate; ++i)
for (int j = i + 1; j < SIZE && !has_duplicate; ++j)
if (streamNum[i] == streamNum[j])
has_duplicate = true;
if (has_duplicate)
{
...
}
else
{
...
}
The first loop goes through each element in the array, the second loop checks if there is a duplicate in the remaining elements of the array (that's why it starts at i + 1). Both loops quit as soon as you find a duplicate (that's what && !has_duplicate does).
This is not the most efficient way, more efficient would be to sort the array before looking for duplicates but that would modify the contents of the array at the same time.
I hope I've understand your requirements well enough.
for(int i=0;i<size;i++){
for(int j=i+1;j<size;j++){
if(streamNUM[i]==streamNUM[j]){
...........
}
}
}
I assume that u need whether there is duplication or not this may be helpful
If not comment
It's a little unclear what exactly you're looking to do here but I'm assuming as it's sudoku you're only interested in storing numbers 1-9?
If so to test for a duplicate you could iterate through the source array and use a second array (with 9 elements - I've called it flag) to hold a flag showing whether each number has been used or not.
So.. something like:
for (loop=0;loop<size;loop++) {
if (flag[streamNum[loop]]==true) {
//duplicate - do something & break this loop
break;
}
else {
flag[streamNum[loop]=true;
}
}
Here's how I'd test against Sudoku rules - it checks horizontal, vertical and 3x3 block using the idea above but here 3 different flag arrays for the 3 rules. This assumes your standard grid is held in an 81-element array. You can easily adapt this to cater for partially-completed grids..
for (loop=0;loop<9;loop++) {
flagH=[];
flagV=[];
flagS=[];
for (loop2=0;loop2<9;loop2++) {
//horizontal
if(flagH[streamNum[(loop*9)+loop2]]==true) {
duplicate
else {
flagH[streamNum[(loop*9)+loop2]]=true);
}
//column test
if(flagV[streamNum[loop+(loop2*9)]]==true) {
..same idea as above
//3x3 sub section test
basecell = (loop%3)*3+Math.floor(loop/3)*27; //topleft corner of 3x3 square
cell = basecell+(loop2%3+(Math.floor(loop2/3)*9));
if(flagS[streamNum[cell]]==true) {
..same idea as before..
}
}

C++ puttting one element of vector to another vector

I am working on a RTS game using SDL. I have a woodyard class whose object will collect wood from nearby trees. In the class I create a vector called temp_trees and as an argument for the constructor I use a vector of tree objects that I pass in.
The woodyard constructor:
woodyard::woodyard(int x, int y, int HP, int id, vector<Tree> trees)
{
...
vector<Tree> temp_trees;
for(int i = 0; i < trees.size(); i++)
{
if((trees[i].xPos - 100) / 50 >= x - 5 && (trees[i].xPos - 100) / 50 <= x + 4)
{
if((trees[i].yPos - 100) / 50 >= y - 5 && (trees[i].yPos - 100) / 50 <= y + 4)
{
temp_trees.push_back(trees[i]);
}
}
}
collect_control = 0;
no = 0;
}
the collect_wood function:
void woodyard::collect_wood(){
if(no == 5)
{
temp_trees[collect_control].drewno -= 1;
if(temp_trees[collect_control].drewno <= 0){
collect_control++;
temp_trees.erase(temp_trees.begin());
}}
no++;
if(no >= 10){
no = 0;
}}
The program crashes just after start.
Can anybody see any errors in this code??
PS: I suppose that there might be something wrong with copping elements from one vector to another in the constructor.
The constructor doesn't contain any illegal operation.
And collect_wood(), although unintelligible, doesn't contain any obvious reason for making it crash.
Which is the value of collect_control? Do you check if it is < temp_trees.size()? Being aware that temp_trees.size() keep changing since you are erasing elements.
Probably collect_control shouldn't be incremented after the erase: all elements shift back, and collect_control after the erase is already pointing to the next element.
Note: Consider that temp_trees.erase(temp_trees.begin()); is one of the most inefficient things you could do with a vector (deleting the first element).
In the woodyard constructor, you are declaring a temporary, function-scoped variable "temp_trees".
woodyard::woodyard(int x, int y, int HP, int id, vector<Tree> trees)
{
...
vector<Tree> temp_trees;
If you have a vector member called temp_trees, this declaration is hiding it. So your member function is NOT seeing the same vector:
void woodyard::collect_wood(){
if(no == 5)
{
temp_trees[collect_control].drewno -= 1;
Also, without seeing the rest of the code, I wouldn't know how you are ensuring that there are at least "collect_control" members in the vector.
#include <assert.h>
...
assert(collect_control < temp_trees.size());
or if you're using visual studio you can do
if(collect_control >= temp_trees.size())
DebugBreak();
"size()" is a 1-based value but array index operators are zero based. That means, when there is one entry in the vector, it will be vector[0]. If the vector is empty, vector[0] is illegal - it does not exist. And emptiness is denoted by size being 0. size must always be greater than the element index you are trying to access.

efficient permuation algorithm for multiple lists

I have a variable number of lists. Each contains different number of elements.
For instance with four lists,
array1 = {1, 2, 3, 4};
array2 = {a, b, c};
array3 = {X};
array4 = {2.10, 3.5, 1.2, 6.2, 0.3};
I need to find all possible tuples whose ith element is from ith list, e.g. {1,a,X,2.10}, {1,a,X,3.5}, ...
Currently I am using a recursive implementation which has performance issue. Therefore, I want to find a noniterative way that can perform faster.
Any advice? Is there any efficient algorithms (or some pseudo code). Thanks!
Some pseudo code of what I implemented so far:
Recusive version:
vector<size_t> indices; // store current indices of each list except for the last one)
permuation (index, numOfLists) { // always called with permutation(0, numOfLists)
if (index == numOfLists - 1) {
for (i = first_elem_of_last_list; i <= last_elem_of_last_list; ++i) {
foreach(indices.begin(), indices.end(), printElemAtIndex());
printElemAtIndex(last_list, i);
}
}
else {
for (i = first_elem_of_ith_list; i <= last_elem_of_ith_list; ++i) {
update_indices(index, i);
permutation(index + 1, numOfLists); // recursive call
}
}
}
non-recursive version:
vector<size_t> indices; // store current indices of each list except for the last one)
permutation-iterative(index, numOfLists) {
bool forward = true;
int curr = 0;
while (curr >= 0) {
if (curr < numOfLists - 1){
if (forward)
curr++;
else {
if (permutation_of_last_list_is_done) {
curr--;
}
else {
curr++;
forward = true;
}
if (curr > 0)
update_indices();
}
}
else {
// last list
for (i = first_elem_of_last_list; i <= last_elem_of_last_list; ++i) {
foreach(indices.begin(), indices.end(), printElemAtIndex());
printElemAtIndex(last_list, i);
}
curr--;
forward = false;
}
}
}
There are O(l^n)1 different such tuples, where l is the size of a list and n is the number of lists.
Thus, generating all of them cannot be done efficiently polynomially.
There might be some local optimizations that can be made - but I doubt swtiching between iterative and (efficient) recursive will do a lot of difference if any, especially if the iterative version is trying to mimic a recursive solution using a stack + loop, which is likely less optimized for this purpose then the hardware stack.
A possible recursive approach is:
printAll(list<list<E>> listOfLists, list<E> sol):
if (listOfLists.isEmpty()):
print sol
return
list<E> currentList <- listOfLists.removeAndGetFirst()
for each element e in currentList:
sol.append(e)
printAll(listOfLists, sol) //recursively invoking with a "smaller" problem
sol.removeLast()
listOfLists.addFirst(currentList)
(1) To be exact, there are l1 * l2 * ... * ln tuples, where li is the size of the ith list. for lists of equal length it decays to l^n.

Shifting elements in an array C++

I've developed a method called "rotate" to my stack object class. What I did was that if the stack contains elements: {0,2,3,4,5,6,7} I would needed to rotate the elements forwards and backwards.
Where if i need to rotate forwards by 2 elements, then we would have, {3,4,5,6,7,0,2} in the array. And if I need to rotate backwards, or -3 elements, then, looking at the original array it would be, {5,6,7,0,2,3,4}
So the method that I have developed works fine. Its just terribly ineffecient IMO. I was wondering if I could wrap the array around by using the mod operator? Or if their is useless code hangin' around that I havent realized yet, and so on.
I guess my question is, How can i simplify this method? e.g. using less code. :-)
void stack::rotate(int r)
{
int i = 0;
while ( r > 0 ) // rotate postively.
{
front.n = items[top+1].n;
for ( int j = 0; j < bottom; j++ )
{
items[j] = items[j+1];
}
items[count-1].n = front.n;
r--;
}
while ( r < 0 ) // rotate negatively.
{
if ( i == top+1 )
{
front.n = items[top+1].n;
items[top+1].n = items[count-1].n; // switch last with first
}
back.n = items[++i].n; // second element is the new back
items[i].n = front.n;
if ( i == bottom )
{
items[count-1].n = front.n; // last is first
i = 0;
r++;
continue;
}
else
{
front.n = items[++i].n;
items[i].n = back.n;
if ( i == bottom )
{
i = 0;
r++;
continue;
}
}
}
}
Instead of moving all the items in your stack, you could change the definition of 'beginning'. Have an index that represents the first item in the stack, 0 at the start, which you add to and subtract from using modular arithmetic whenever you want to rotate your stack.
Note that if you take this approach you shouldn't give users of your class access to the underlying array (not that you really should anyway...).
Well, as this is an abstraction around an array, you can store the "zero" index as a member of the abstraction, and index into the array based on this abstract notion of the first element. Roughly...
class WrappedArray
{
int length;
int first;
T *array;
T get(int index)
{
return array[(first + index) % length];
}
int rotateForwards()
{
first++;
if (first == length)
first = 0;
}
}
You've gotten a couple of reasonable answers, already, but perhaps one more won't hurt. My first reaction would be to make your stack a wrapper around an std::deque, in which case moving an element from one end to the other is cheap (O(1)).
What you are after here is a circular list.
If you insist on storing items in an array just use top offset and size for access. This approach makes inserting elements after you reached allocated size expensive though (re-allocation, copying). This can be solved by using doubly-linked list (ala std::list) and an iterator, but arbitrary access into the stack will be O(n).
The function rotate below is based on reminders (do you mean this under the 'mod' operation?)
It is also quite efficient.
// Helper function.
// Finds GCD.
// See http://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations
int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}
// Number of assignments of elements in algo is
// equal to (items.size() + gcd(items.size(),r)).
void rotate(std::vector<int>& items, int r) {
int size = (int)items.size();
if (size <= 1) return; // nothing to do
r = (r % size + size) % size; // fits r into [0..size)
int num_cycles = gcd(size, r);
for (int first_index = 0; first_index < num_cycles; ++first_index) {
int mem = items[first_index]; // assignment of items elements
int index = (first_index + r) % size, index_prev = first_index;
while (index != first_index) {
items[index_prev] = items[index]; // assignment of items elements
index_prev = index;
index = (index + r) % size;
};
items[index_prev] = mem; // assignment of items elements
}
}
Of course if it is appropriate for you to change data structure as described in other answers, you can obtain more efficient solution.
And now, the usual "it's already in Boost" answer: There is a Boost.CircularBuffer
If for some reason you'd prefer to perform actual physical rotation of array elements, you might find several alternative solutions in "Programming Pearls" by Jon Bentley (Column 2, 2.3 The Power of Primitives). Actually a Web search for Rotating Algorithms 'Programming Pearls' will tell you everything. The literal approach you are using now has very little practical value.
If you'd prefer to try to solve it yourself, it might help to try looking at the problem differently. You see, "rotating an array" is really the same thing as "swapping two unequal parts of an array". Thinking about this problem in the latter terms might lead you to new solutions :)
For example,
Reversal Approach. Reverse the order of the elements in the entire array. Then reverse the two parts independently. You are done.
For example, let's say we want to rotate abcdefg right by 2
abcdefg -> reverse the whole -> gfedcba -> reverse the two parts -> fgabcde
P.S. Slides for that chapter of "Programming Pearls". Note that in Bentley's experiments the above algorithm proves to be quite efficient (among the three tested).
I don't understand what the variables front and back mean, and why you need .n. Anyway, this is the shortest code I know to rotate the elements of an array, which can also be found in Bentley's book.
#include <algorithm>
std::reverse(array , array + r );
std::reverse(array + r, array + size);
std::reverse(array , array + size);