![Quote](https://cboard.cprogramming.com/images/misc/quote_icon.png)
Originally Posted by
CH1156
I want to remove every element where the tuple has a value of 0 for the second element of the tuple
For that, the typical approach is to #include <algorithm> and write:
Code:
mInventory.erase(
std::remove_if(
mInventory.begin(),
mInventory.end(),
[](const auto& item) { return std::get<1>(item) == 0; }
),
mInventory.end()
);
remove_if is used because now you want to remove elements from the vector if they satisfy the condition specified in the lambda. However, remove_if "removes" by shifting the elements not removed to the front of the range, returning an iterator to one past the end of the new valid range. So we take this iterator and pass it to mInventory's range-based erase member function, so that it can erase those "removed" elements.
This approach is suitable if you want the player to be able to use multiple items and have their counts set to 0, and then later you perform clean up to erase all these zero-count items. It wouldn't make sense to use it for the scenario you described in post #6 ("if the player has 3 weak potions and they use alll of them, how would I remove the tuple element of that item") because it would be overkill, i.e., you'll be removing all zero-count items (and you don't know which ones they are) when there is exactly one zero-count item and you know exactly what it is.
![Quote](https://cboard.cprogramming.com/images/misc/quote_icon.png)
Originally Posted by
CH1156
This works but i dont know if there is some hidden problems with it. Please let me know if there is.
The "hidden" problem is that you're using the choice variable. In this scenario of removing every element, the choice variable conceptually does not exist: there is no choice because if you're zero, you're removed, no appeal granted. The player is not allowed to choose anything (those choices have already been made and the items used, so the time to choose has passed), hence what you're doing is conceptually wrong.
However, if you're trying to solve for the problem of "if the player has 3 weak potions and they use alll of them, how would I remove the tuple element of that item", then this is correct, because at this point, the player has chosen the weak potion, and you check its count after usage and find that it is zero, so you erase it. That's what I was talking about in my previous post.