I am reposting this question as I fixed it now to make it easier to understand exactly what I need to do.
I have a function declared:
set<string> findQueryMatches(map<string, set<string>>& index, string sentence);
The map is an already filled map with keys and values, while the string will be a sentence that will look something like this "fish +red". The keys and values for the map come from a file that I read in previous functions with an example being:
www.shoppinglist.com
EGGS! milk, fish, @ bread cheese
www.rainbow.org
red ~green~ orange yellow blue indigo violet
www.dr.seuss.net
One Fish Two Fish Red fish Blue fish !!!
www.bigbadwolf.com
I'm not trying to eat you
The website names are the values while the separate words (also have a clean token function that cleans punctuation, so egg! becomes egg and all the weird symbols get deleted) are keys for the map. So if you search for fish, you get the list of values for that keyword.
In the above function SearchQueryMatches, I input a string sentence which has to handle the terms as a compound query, where individual terms are synthesized into one combined result.
The entered string will possess whitespaces, +, and -. a minus means a result must match one term without matching the other, a plus means the results must match both items, while a white space without any preface means they are unioned, so they match one or the other.
For example,
"tasty -mushrooms simple +cheap" translates into "tasty WITHOUT mushrooms OR simple AND cheap"
I started by doing stringstream that separates the sentence and then did if statements like
if (word[0] == '+').....
After I separate these words and know what to do with them I will also have to call my helper clean function again to clean them up from the + and - before I begin the search.
But now, I am struggling with what I would need to do next. I heard of set_intersection functions from the C++ set library but I never used them and honestly have absolutely 0 idea on how to use it.
The return will be a set of the websites that satisfy the search query.
What would be a good way to program the inside of the if statements with what they would be doing each time there is a +, -, or no preface? I am completely lost on this.
Certainly you can solve the problem using set_intersection, set_difference and set_union. And here is a example about how to use those functions in your problem:
Keep in mind that you have to provide an output operator to set_intersection, set_difference and set_union (look at this: https://en.cppreference.com/w/cpp/algorithm/set_difference or this how to find the intersection of two std::set in C++?). Those output operators can be defined of this way:
For example given this data:
And this sentence:
The result is:
Cheers!