Thread: How to get the index of a vector of tuples

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote 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 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.
    Last edited by laserlight; 03-08-2021 at 02:06 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-08-2016, 08:39 PM
  2. How to find Index positions with a vector STL?
    By codeguy in forum C++ Programming
    Replies: 6
    Last Post: 01-11-2008, 01:07 PM
  3. How to get value at particular index from a vector?
    By ketu1 in forum C++ Programming
    Replies: 14
    Last Post: 01-03-2008, 01:53 PM
  4. How to find index of a particular record in a vector?
    By ketu1 in forum C++ Programming
    Replies: 7
    Last Post: 01-02-2008, 12:22 PM
  5. Illegal vector index problem
    By Maiq in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2003, 06:07 PM

Tags for this Thread