STL functions which work on vectors

Is there a document or book which has a list of all the stl functions ?
I need stl functions
1) I have a vector of an ItemSold object with name and qty(across multiple stores) , how can I get distinct namess with stl ? Or is it a better way to do it other than vectors ?
Object has store name , location , product , qty available and rate
2) I have a list of ShoppingList object . Members being item name and qty needed .
3) I have to get the distinct items across stores
4) items and how many store sells it
4) make a shopping list losing where can I get the items for least price .
I may need 10 items and the least price store may have only 4 item and I may have to go to multiple stores to get the 10 .
Low price is the objective , many store visit is ok .
Where all I can use stl ? I made a vector of store items and vector of items needed to buy .
<set> discards duplicates but may not be useful for this problem. A <map> or unordered map where the thing being duplicated is the *key* can also be made to work (you can update the data if you get that key again), but again, it may not be a great setup.

your <vector> may not be properly configured to solve the problem you want to solve.
consider this: vector<map <key, data >> ... hmm. The vector would represent stores. The map would represent an item (the key) and its fields (quantity, price, stuff).
then for example just ask
"I need 10 whatzits"
for(all the stores)
store[index][item].quantity... and .price ... some logic gets you the answer with minimal fuss.
you can also make store a map if you have like a store ID instead of using 0-N for a vector.

the stl is full of algorithms, but the data structures are your ticket to winning. Using maps avoids searching and some of the iteration, for example. You don't need 'find' (useful for a vector) because you have a key that just goes to the desired data. You don't need sort, because the data is organized reasonably well already ... BUT... one last thing...
a container of pointers is cheap. Its cheap to create and manage. So lets say you DO want to sort by lowest price, then by quantity, using std::sort with your own comparison function (which is like any other except is price is equal, the highest quantity one is 'lower' so it will appear first). Ok, we can do that. Iterate the data, pull what you need into a temporary vector, but don't copy the objects, just take pointers to them. Then sort and extract what you need. Sorting pointers is cheap too: its just an integer, really, not big objects that have a hefty copy cost. If you decide to play with that idea, remember to keep redundant back tracking info, here, that would be the store's data being placed in with the item data (redundant, its also at the store level).
Last edited on
2 Stores:


I have to print

There are 2 distinct item(s) available for purchase.
There are 10 Apples(s). (in alphabetical order by item name across stores)
There are 8 Candy(s).

I created a structure called Product which has
Store name, location, item name,qty,rate

struct Product {
    string store;
    string loc;
    string name;
    string qty;
    string price;

    Product (string s, string l, string n, string q, string p) : 
    store(s), loc(l), name(n), qty(q), price(p) {};

I have to maximize the use of STL for this.
I am thining accumulate, copy if but not sure how to get it working.
Use of map would be logical, but then you don't need any std algorithm on vector:
map<string,integer> statistics
FOR p in products
    statistics[] += p.qty as number

print There are statistics.size() items
FOR item in statistics
    print There are item->second item->first
Last edited on
Topic archived. No new replies allowed.