Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
While I'm aware that this is just a simple thing to do, I've tried to make this into a function. While mine kind of works, I'm more interested in how other people would go about it.
C++
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

bool restart = true;
std::string choice;

bool inInventory(std::vector<std::string> vec, std::string inItem) {
    std::vector<std::string>::iterator invent;
    invent = std::find(vec.begin(), vec.end(), inItem);
    if (invent != vec.end()) {
        return true;
    } else {
        return false;
    }    
}

void dropItem(std::vector<std::string> vec, std::string itemToDrop) {
    std::vector<std::string>::iterator invent;
    invent = std::find(vec.begin(), vec.end(), itemToDrop);
    if (inInventory(vec, itemToDrop)) {
        std::remove(vec.begin(), vec.end(), itemToDrop);
    } else {
        std::cout << "Item is not in vector!\n";
        restart = true;
    }
}

int main() {
    
    std::vector<std::string> shoppingList;

    shoppingList.push_back("Butter");
    shoppingList.push_back("Cheese");
    shoppingList.push_back("Candy");
    while (restart) {
        restart = false;
        for (auto e: shoppingList) {
            std::cout << e << ", ";
        }
    
        std::cout << "Pick an item to drop\n>";
        std::cin >> choice;
        dropItem(shoppingList, choice);
    
        for (auto e: shoppingList) {
            std::cout << e << ", ";
        }
        return 0;
    }
}

Here is the code in case anyone is interested.

BTW: I'm not requesting anyone to fix this. I'm just wondering how the process would go for those more skilled than me.

Thanks!

What I have tried:

Messing around with functions and vectors, mainly the std::remove(); function.
Posted
Updated 12-Sep-23 11:32am
v3

That is done use the erase method of the vector class : https://cplusplus.com/reference/vector/vector/erase/[^]. It can be a very expensive operation to perform if deleting from anywhere except the tail end of the vector. If deletions are going to be common then you should consider using a std::list or std::deque. Both of those accommodate additions and deletions very well. The downside is you can't access elements quite as easily as you can with a vector. They support iterators like vector does though so that style of access is consistent. For vectors of simple types deletion is not terribly expensive although it requires reallocation and copying of data. That is why it is expensive for types that are more complex.

Here is documentation on those classes :
https://cplusplus.com/reference/list/list/[^]
https://cplusplus.com/reference/deque/deque/[^]
 
Share this answer
 
Comments
CPallini 12-Sep-23 3:36am    
5.
Brennon Nevels 12-Sep-23 5:34am    
Would the std::string type be considered a simple type?
Rick York 12-Sep-23 11:16am    
It is on the border line but, for the most part, yes.
With C++ 20, the single line
C++
std::erase(shoppingList, choice);
would do the trick. See std::erase, std::erase_if (std::vector) - cppreference.com[^].
 
Share this answer
 
Comments
Brennon Nevels 12-Sep-23 5:33am    
5
CPallini 12-Sep-23 5:41am    
Thank you.
If the order of elements in the vector isn't important, an efficient way to do this is to move the last element into the slot of the element being erased. This is how I manage the list of open sockets for the poll function, and I wrote a simple article describing a class template that does this while otherwise reusing std::vector:

A Wrapper for std::vector[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900