This question already has answers here:
Give me a practical use-case of Multi-set
(4 answers)
"multiset" & "multimap" - What's the point?
(7 answers)
Closed 2 years ago.
I understand the usage on sets in C++, but why do multisets exist?
What are some real world applications where multisets are useful?
This argument can extended for unordered multisets as well, what differentiates then from using a vector and what advantages and disadvantages does it provide?
Because you don't have to store single-element objects in a multi-set. You're thinking of storing something like a string in a multi-set. But that's not what it's made for. You can have any struct you want, and make the comparison be with a single element in the struct.
For example:
struct PhoneBookEntry
{
std::string name;
std::string phoneNumber;
}
In this naive "phone book" entry, there's no reason to have a single entry per name in a phone book. There might be many. So you make a multiset of PhoneBookEntry, and you make the comparator be by name. This way, you can have multiple phone numbers with the same name.
Now you might think that a map is more suitable for this, sure. But this is just an example. If you have a structure where you don't need a key/value but you need the search properties of a set with multiple elements per key, you use a multiset.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
The community reviewed whether to reopen this question 6 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I made a program recently that dealt with a lot of if/else statements to return particular values. Someone recommended using lookup tables instead. My question is,
How do they work and how do you implement them?
What is the difference between a map, hash table, and lookup table.
My question is, how do they work and how do you implement it? What is
the difference between stl map, hash tables, and lookup tables.
What you're looking for is an efficient mechanism by which you can look up the value that corresponds to a given key.
Your current mechanism (a long list of if/else-if commands) is rather inefficient, in that if you have N possible values to choose from, you will (on average) have to compare your candidate key against (N/2) other keys before you find the one that matches and you can stop looking. (This is known as O(N) complexity)
So what are the other choices?
The simplest one is literally just an array of values, e.g.
const char* const myLookupTable[1000] = {
"zero",
"one",
"two",
[...]
"nine hundred and ninety-nine"
};
... with a lookup table like that, you take a key (which in this case is a number between 0 and 999, inclusive), and look up the corresponding value with a single array-lookup:
const char* val = myLookupTable[myKeyIndex];
That's super-efficient (O(1) complexity -- it always finishes in constant time, regardless of how big the array is!), but it only works in cases where your keys are unsigned integers in a continuous (and relatively small) range of values. For example, if your keys were strings, this approach wouldn't apply.
For more flexibility, the next option would be STL's std::map. std::map gives you fast key->value lookups from any key-type to any value-type. Internally it is implemented as a tree: each key-value pair is inserted into the tree in such a way that the tree remains sorted with the smallest keys at the left of the tree and the largest keys at the right. Because of that, looking up a key (and its associated value) in a std::map is just a matter of starting at the tree's root node and comparing the key at that node to the key you are looking up: is it less than your key? Then move to the right-hand child. Or it greater than your key? Then move to the left-hand child. Repeat that until you get to the bottom of the tree, at which point you'll either find the key-value pair you were looking for or you'll find that it's not present. This is an algorithm of O(log(N)) complexity, because for a tree with N values in it, it takes log(N) comparisons for the lookup to complete. O(log(N)) is considered pretty good efficiency.
The final data structure you mentioned is a hash table (as seen in std::unordered_map). A hash table does things a bit differently -- internally it is an array, but in order to avoid the limitations of the lookup-table approach, it also comes with an algorithm for figuring out where in its array a given key/value pair is to be stored. It does this by calculating a hash code for the key-object you pass in -- and then using that code to compute an offset into the array (e.g. int array_offset = hash_code % array_size) and looking at that slot in the array to see if the requested key-value pair is there. If it is, then it's done (O(1) performance again!); or if the slot is empty, then it knows that your key isn't in the table and can return failure immediately (O(1) again). If the slot is occupied by some other key/value pair, then the hashtable will need to fall back to another algorithm to sort out the hash collision; different hash tables handle that different ways but it's generally still fairly efficient.
Your question is really to broad since StackOverflow is not a tutorial site, but I feel kind this morning...
A "lookup table" is simply a container (any kind of container) that contains values you look up, and usually map to some other value.
In its simplest form, consider the following:
struct MapIntToString
{
int value;
char* string;
};
MapIntToString my_map[] = {
{ 1, "one" },
{ 2, "two" },
{ 3, "three" },
// ...
};
The above could be considered a lookup table. You can iterate (loop) over my_map to find (look up) the integer 2 and then pick the string "two" from it.
Depending on your need and use-case the above example might not be enough. The code above is basically how it is commonly done in plain C, not C++. For C++ there are better containers for mapping values, like std::map and std::unordered_map.
However sometimes the standard types might not be enough, and there are many other data-structures that could be implemented for looking up things.
This question already has answers here:
How can I efficiently select a Standard Library container in C++11?
(4 answers)
Closed 4 years ago.
I have several items saved in a list. I would like to add items that have already been processed to a datastructure (this makes sense in my case even though you might wonder why). When processing the next item from the list I first want to make sure if it has been processed before so lets say something like this:
if(element_is_in_datastructure(current_element)) {
do this
}
else
{
do that
add_element_to_datastructure(current_element)
}
My question is, what is the ideal datastructure where checking if the element is in it won't take too long. At the moment I don't have too many elements (max 30) which will be added to the datastructure, but this number might increase and I don't want to lose performance.
You can use a map e.g std::unordered_map to store your elements as keys.
Then just check their presence e.g
if(!yourMap.count(element))
{
// your element is not in the structure
}
This finding takes logarithmic time in the map's size to finish.
This question already has answers here:
Accessing a class member variable by its name at runtime [duplicate]
(3 answers)
Closed 5 years ago.
this is for a personal use application in c++
for example:
class x
{...};
int main()
{
string userinput;
cin>>userinput;
cin>>x."userinput";}
is something like this possible?
If what you are talking about is allowing for the user to set variable names at runtime, then no. It is not possible in c++. Depending on what you are trying to achieve, there are a few ways around this.
If you just need the user to be able to access a small number of variables, all of which are known before hand, you can use a switch statement, or a bunch of if/elseifs
If you need to store a larger amount of variables the number of which may not be known at compile time, then there exists a number of data structures that exist just for this purpose. There are arrays, hash tables, linked lists and hundreds of variations on the above. These are all far too complex topics to cover in a single answer however.
This question already has answers here:
Sorting a std::map by value before output & destroy
(5 answers)
Closed 8 years ago.
I have a need to sort a Map(Key=Word,Value=CountofWord) in the descending order of the count of occurrences and print top 10 words in the Map. What can be the best possible method to do that?
My idea is to build a Multimap(Key=CountofWord,Values=Words) and then print the top 10 elements of the Multimap. But this would take O(n) extra space.
Can there be any other optimized solution?
Just to point out that your proposed solution does not take into account that two counts of word can have the same value, therefore two words would be mapped to the same key and override the last one.
What you could do instead is make a class of a word with member variables the word as string and the count as int, and implement the < operator to allow sorting of the objects by the std::sort method. Refer to http://www.cplusplus.com/reference/algorithm/sort/.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find an item in a std::vector?
I am using C++ Builder to create a VCL Forms application. I also have a vector array of appointment objects that each have a name, type, reminder date/time, a date/time, location and comments.
I am wanting to implement a find feature that will let a user find an appointment given certain criteria.
The user can choose to find an appointment in the vector array by either choosing the name, type etc or a combination of each.
What would be the best programming concept to use for this situation? The vector is not large. No more than 10 or 20 elements.
Thanks
Use std::find_if() and define the required predicate (if C++11 you can use lambda function).
See online demo http://ideone.com/Md7sp.
std::find_if(A.begin(),A.end(),isthatit(conditions));
where isthatit is a predicate object constructed from conditions.
If you have many criterias you should think about
Boost.MultiIndex container which targets different search indexes.
http://www.boost.org/doc/libs/1_51_0/libs/multi_index/doc/tutorial/index.html