Reputation: 756
Sets are used to get information of an object by providing all the information, usually used to check if the data exists. A map is used to get the information of an object by using a key (single data). Correct me if I am wrong. Now the question is why would we need a set in the first place, can't we a map to see if the data exist? why would we need to provide all the information just to see if it exist?
Upvotes: 1
Views: 992
Reputation: 42072
A set
serves to group items of the same type
that are different among themselves (i.e., they are not equal
). For example, the numbers 1
and 2
are both of int
type, but 1!=2
.
set
containers are useful when you want to keep track of collections of homogeneous things as a group, and perform mathematical operations on such groups (like intersection, union, difference, etc). For example, imagine a set
of search results containing all the documents mentioning the words cat
and dog
. And then another set
containing all the documents mentioning the words pet
. The union
of those two sets would give you the group of documents containing the words cat
, dog
, and pet
. Notice that such group will have no repetitions (i.e., if a document was in the both sets initially, it will be only once in the second set).
maps
are most certainly not a set
, but they can be seen as an arrangement which allows you to associate a value with every element of a set
. They are used to represent relationships. For example, the set
of people working for a company have an associated employee_number
; in this case a map
would be a useful structure to represent such relationship.
Going back to the previous example, if you wanted to know how many times has each page been accessed, you could probably create a map along the lines of std::map<Page, int>
, that is, a relationship between the pages, and the number of times each has been visited.
Notice that the keys of a map form a set
(probably this is what confuses many people), and an implication of this property is that you can only have a given key once (there are some esoteric containers where a key can be mapped to different values though).
So, if you need to interact with groups and collections as a whole, and with the members of the group itself, probably you want a set
. If you need to associate certain things with members of a group or a collection, probably you want a map
. If the association spans more than one dimension, probably you want a multi_map
.
Important notice that in C++ std::set
and std::map
are ordered. C++11 offers alternative unordered containers called std::unordered_set
and std::unordered_map
.
Upvotes: 1
Reputation: 38126
There are many operations where you just need a set. Using a map would be just extra space.
Upvotes: 2
Reputation: 27599
A Set contains a unique list of ordered values, but a Map can contain a non unique set of unordered values accessed using a key.
Either could be used to determine if an object exists, it depends on your use case and how you need to be able to access that object - can you test to see if the Set contains an object that you have a reference to, or do you need to look it up by one or more keys to be able to compare it?
Upvotes: 0