ElusiveTau wrote: |
---|
std::set seems redundant and behaves a lot like a std::map, except without the "map property". |
It's true. You could just use a std::map, but if you don't need a "value" for each "key" then it's often more convenient to use a std::set.
Despite the similarities I often find the usage to be a bit different (at least conceptually).
I use a std::map when I want to look up an object by key.
I use a std::set when I want to keep track of if a value is in a "set" (similar to how you would ask if an element is in a mathematical set or not).
ElusiveTau wrote: |
---|
Was std::set suppose to model mathematical sets? |
I don't think it was invented to be used for mathematical programming. It was invented because it's a useful container and the name "set" makes sense because it has many of the same properties as a mathematical set.
ElusiveTau wrote: |
---|
What's strange is that the (mathematical) set operations that would distinguish a set from a map aren't built-in |
Those are the things you can
do with a set, but what actually
is a set?
Reading the first paragraph on Wikipedia that describes what a set is...
https://en.wikipedia.org/wiki/Set_(mathematics)
Wikipedia wrote: |
---|
A set is the mathematical model for a collection of different things; |
Okay...
Wikipedia wrote: |
---|
a set contains elements or members, which can be mathematical objects of any kind: numbers, symbols, points in space, lines, other geometrical shapes, variables, or even other sets. |
You can store any type of object in std::set (as long as it can be compared using < or if you provide a custom comparator).
Wikipedia wrote: |
---|
The set with no element is the empty set; |
A std::set can be empty and it's even got an empty() member function that will return true if this is the case.
Wikipedia wrote: |
---|
a set with a single element is a singleton. |
Okay, a std::set containing 5 is not the same as 5.
Wikipedia wrote: |
---|
A set may have a finite number of elements or be an infinite set. |
std::set can only have a finite number of elements for obvious reasons.
Wikipedia wrote: |
---|
Two sets are equal if they have precisely the same elements. |
This is true for std::set.
jonnin wrote: |
---|
see std::set_union or std::set_intersection for those.
subset can be done via std::includes, yes the name is weird, but its what you seek. |
Note that these functions work only on sorted ranges. This means they will indeed work for std::set but not for std::unordered_set.
The name "includes" is not that weird because it returns true if the elements of the second set is included in the first set.
Wikipedia wrote: |
---|
If every element of set A is also in B, then A is described as being a subset of B, or contained in B, written A ⊆ B, or B ⊇ A. The latter notation may be read B contains A, B includes A, or B is a superset of A. |
https://en.wikipedia.org/wiki/Set_(mathematics)#Subsets (Bolding by me)
Example:1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <set>
#include <algorithm>
#include <iostream>
int main()
{
std::set<int> set1 = {1, 2, 3, 4, 5};
std::set<int> set2 = {2, 4};
bool set1_includes_set2 = std::includes(set1.begin(), set1.end(), set2.begin(), set2.end());
std::cout << std::boolalpha;
std::cout << set1_includes_set2 << "\n";
}
| |
Output:
The thing that surprises me is that it hasn't got "set" in its name like the other "set operations" in the <algorithm> header.
https://en.cppreference.com/w/cpp/header/algorithm#Set_operations_.28on_sorted_ranges.29_2
jonnin wrote: |
---|
The c++ naming conventions are frequently a complete failure: map isn't a hash map exactly, set isnt a math set, vector isnt a math vector, cmath isnt C code, and there are more esp when trying to do mathy things. |
Personally I'm not bothered by the name "set".
Why would you associate the word "map" with a hash map/hash table specifically?
Both std::unordered_map (implemented as a
hash table) and std::map (implemented as a
binary search tree)
map values to other values/objects.
Similarly, Java (another programming language) has both HashMap and TreeMap (which both implement the Map interface).
Note that the word "map" is also used in mathematics:
https://en.wikipedia.org/wiki/Map_(mathematics)
I agree that "vector" is unfortunate, but mainly because it's relatively common to want to use real mathematical vectors in programming. Not everyone use them but the people that do often use them a lot.
The
c in "cmath" means that it's based on the C header named "math.h". The same naming convention is used for all headers that come from C. This decision was done a long time ago. For many of these headers there exist C++ alternatives that are often recommended to be used instead but that's not the case for cmath.
jonnin wrote: |
---|
c# is the grandchild of c++. |
Is it? Ignoring the name, what makes C# more related to C++ than for example Java? (you don't need to answer)