← Back to STL Containers
Question 477
Inventory with multiset
Inventory with multiset
Build a classInventorythat stores item names in amultiset<string>, so the same name can be held several times. Public methods: 1.void add(const string& name): adds one copy. 2.bool removeOne(const string& name): removes exactly ONE copy and returns true; if the item is absent, returns false and changes nothing. 3.int count(const string& name) const: how many copies are stored (0 if none). 4.void print() const: one line per distinct item in alphabetical order, asapple x2(name, space,x, count). An empty inventory prints the single line(empty). The test printstrue/falsefor eachremoveOneand the number fromcount. For add apple, pear, apple;print();removeOne("apple");removeOne("kiwi");print()(see Expected Output). Do NOT write a main function.
Expected Output:
apple x2 pear x1 true false apple x1 pear x1 2 true true 0 false (empty)
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersSets
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.