Wondering what kind of data structure to use for a project

Pages: 12
Hello, I am getting back into programming after many years off the grid.

I am a cook and I want to make a program that stores and retrieves food pairings.

I am wondering what a good data structure to use would be? Arrays seem to be limited by their static size although if I am reading and writing from a text file maybe I can create them and destroy them as needed? I have some experience using struct. I have no experience using linked lists, hash tables or anything like that but they seem interesting. I started taking some online courses but there is so much irrelevant information involved that it makes my head spin and most of them don't relate to saving and retrieving information effectively.

For example I would like to store under
APPLE: caramel, ginger, cinnamon, cloves, nutmeg etc.

and be able to add or eventually subtract items at any time.

Some direction would help, thank you
My best bet would be making a class to hold and return all the information you need. Inside the class you could have structs for each ingredient, for instance here apple, and within it, a vector of strings to hold the names of other ingredients you want paired with it. You could of course add and delete items from said struct but you would have to read from a file when you run the program and save to the file every time you change anything.

That's probably how I would go about doing what you said. I hope that helps :)
Not buying the "cook" story, but a map of string to vector of strings might do.

Input file:
apple: caramel, ginger, cinnamon, cloves, nutmeg
orange: dirt, turd


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>

using Vec = std::vector<std::string>;
using Map = std::map<std::string, Vec>;

int main() {
    using std::string;
    using std::getline;

    Map food;
    std::ifstream fin("food.txt");

    for (string line; getline(fin, line); ) {
        std::istringstream iss(line);

        string word;
        getline(std::ws(iss), word, ':');

        Vec v;
        for (string w; getline(std::ws(iss), w, ','); )
            v.push_back(w);
        food.insert(std::make_pair(word, v));
    }
   
    for (const auto& p: food) {
        std::cout << p.first << '\n';
        for (const auto& s: p.second)
            std::cout << "  " << s << '\n';
    }
}

<maps> are ideal. In fact they are colloquially known as 'dictionaries'. It might even be simply a matter of storing a single string against the key rather than using a <vector>, whatever.

( Something tells me the dutch-apple on the menu might be rather more palatable than the orange, despite the dutch connection there. )
Not buying the "cook" story
Now that I think about it...
need a solid working define for what a food pairing means. I know what it means in english, but not what YOU mean by it in terms of your program design.

It sounds like you need something capable of a many to many relationship. What sort of info do you want back? do you want to know that apples pair with pork which pairs with bbq sauce and onions, and get feedback that apples and bbq sauce is a no-go? or it it simpler, like red wine and a steak?

And, does chef have 100000000000000 ingredients where you can select up to 1000 of them to form a dish and need to know if the dish is tasty? (This isnt aimed at your question, its just a running forum joke about a low quality code competition site).
Last edited on
If Apples pair with Ginger then Ginger pairs with Apples too, so I'd change Dutch's program to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>

using Vec = std::vector<std::string>;
using Map = std::map<std::string, Vec>;

int main() {
    using std::string;
    using std::getline;

    Map food;
    std::ifstream fin("food.txt");

    for (string line; getline(fin, line); ) {
        std::istringstream iss(line);

	string item1, item2;
        getline(std::ws(iss), item1, ':');

        while (getline(std::ws(iss), item2, ',')) {
	    food[item1].push_back(item2);
	    food[item2].push_back(item1);
	}
    }
   
    for (const auto& p: food) {
        std::cout << p.first << '\n';
        for (const auto& s: p.second)
            std::cout << "  " << s << '\n';
    }
}

Input:
apple: caramel, ginger, cinnamon, cloves, nutmeg
orange: dirt, turd
coffee: cinnamon, nutmeg
ice create: caramel, coffee


Output:
apple
  caramel
  ginger
  cinnamon
  cloves
  nutmeg
caramel
  apple
  ice create
cinnamon
  apple
  coffee
cloves
  apple
coffee
  cinnamon
  nutmeg
  ice create
dirt
  orange
ginger
  apple
ice create
  caramel
  coffee
nutmeg
  apple
  coffee
orange
  dirt
  turd
turd
  orange

You might also have an input file of items that don't go well together.
What’s ‘ice create’?
Must have meant "cream" but his fingers typed "create", which is an interesting typo. His mind was clearly in programming mode. :)
Makes sense.

I was thinking (the worst perhaps) and it was some sort of methamphetamine concoction in which case your suspicions about the cook thing were well founded :(
meth: ephedrine, muriatic acid, lye, ethyl ether
Yum, eat up quickly, I can hear sirens ...

:)
Thank you for the tips! I am definitely looking into vectors and maps.

It sounds like you need something capable of a many to many relationship. What sort of info do you want back? do you want to know that apples pair with pork which pairs with bbq sauce and onions, and get feedback that apples and bbq sauce is a no-go? or it it simpler, like red wine and a steak?


Eventually I would like to incorporate more complex relationships like what you suggest. For now I just want to be able to search a database of foods and their pairings. I was stuck wondering what data type would be best for being able to add and subtract entities from each entry. If you have input on how to cross reference later I would take it so I can start thinking about it. Eventually I want to incorporate nutritional information and have a program that helps people make decisions on what the eat based on nutrition, taste and user preferences. I am using the idea to learn the language so I know I will probably have to start from scratch several times as I advance but I want to take it step by step and learn the concepts as I go along.

I ordered a more up to date book to work out of since the one I have is old and has nothing to say about vectors or maps.
Yum, eat up quickly, I can hear sirens ...

That's just the buzzing in my ears. Anyway, who hasn't accidentally whipped up a batch of methamphetamine when baking a cake?
https://www.geeksforgeeks.org/map-associative-containers-the-c-standard-template-library-stl/

The well known food category provides plenty of scope for automation:

food groups - carbohydrates, protein, milk and dairy, fruit & vegetables, fats & sugars

tastes - bitter, sour, salty, sweet, and umami (who knows where chilli hot fits in?)

and then there are balanced diets - recipes and meal plans
There's not only "false heat", like spiciness, but "false cold" like menthol.

And apparently there's evidence for a "sixth taste" for fat. I think it's called ohfatass.
The sensation of spicy hot food is called "pungency", it neither a taste nor an aroma but an irritation.

I think there is something to the idea of fat having a taste but it is basically sweet and savory in my book.
ohfatass
is possibly not to a lot of people's taste which is why we don't hear of it often. Well certainly in my area. Is it usually served hot, warm or cold, may I ask?
1
2
3
4
5
6
7
8
9
namespace spicy_food_tips
{
/*
If the chilli you've just eaten is so f'kin hot you
can't take the heat any longer, don't drink water,
drink milk because the capsaican in chilli reacts
with the casein in milk to destroy the heat.
*/
}
Last edited on
Is it usually served hot, warm or cold, may I ask?

Muy caliente.

Paradoxically, used topically capsaicin aids in controlling peripheral nerve pain.

Casein (from Latin caseus "cheese") are proteins commonly found in mammalian milk, comprising about 80% of the proteins in cow's milk but only between 20% and 45% of the proteins in human milk. So choose your udder wisely.
Last edited on
Pages: 12