Hey guys im suppose to be creating a bag adt for a code that a professor has already created. i am having troubles with deleting a certain element in a array and it is saying that it is having troubles converting the function of remove and contains. if anyone could help that would be nice Thanks in Advance!
since i need pointers is there a way of adding a pointer to be able to use /code delete /endcode. also my complier isnt liking the != and the == with NULL. it gives the error of no match for 'operator!=' or 'operator==' thanks!
You don't need pointer and they would not help anyway.
it gives the error of no match for 'operator!=' or 'operator=='
Because strings are not pointer.
One way to remove an element from an array:
1 2 3 4 5 6 7 8 9 10 11 12
bool Bag::remove(string item){
bool is_found = false;
for (int i=0; i < n; i++){ // Note: use n here
if (list[i] == item){ // Note: ==
swap(list[i], list[n-1]); // Note: This will change the order, otherwise you need to copy from list[i + 1] ... list[n-1] -> list[i] <-> loop
--n; // Note: Important! This reduces the array size
is_found = true;
break;
}
}
return is_found;
}