What is a "set" in C ++? When are they helpful?

I'm having difficulty conceptualizing C ++ sets.

What is it? How are they useful?

+4
source share
8 answers

Do not feel bad if you have problems understanding sets in general. Most of the math is spent in terms of set theory:

http://en.wikipedia.org/wiki/Set_theory

Think of a collection as a collection of unique, disordered objects. In many ways, it looks like a list:

{1, 2, 3, 4}

but the order is unimportant:

{4, 3, 2, 1} = {1, 2, 3, 4}

and repetitions are ignored:

{1, 1, 2, 3, 4} = {1, 2, 3, 4}

The C ++ collection is an implementation of this mathematical object with an odd function that is sorted inside. But this is just an implementation detail and has nothing to do with understanding the data structure. Sorting is for speed only.

+25
source

C ++ STL Sets are associative mappings that guarantee both sorting and uniqueness of elements in a set (multi selectors guarantee the first, but not the last).

They are commonly used as part of defined operations โ€” things like unions, intersections, and other interactions, including the inclusion / exclusion of elements within a set.

+5
source

"Set" is a kind of collection in which several unique objects are stored. This is useful when you want to collect objects, but you donโ€™t like their order or how many times the same object is in it.

See this for more details: Install in C ++

+3
source

Attitudes "in general" are a (very fundamental) concept in mathematics.

STL set based on the mathematical concept of recruitment: it is a collection of unique members or "Unique associative container" in STL terminology. One strange thing is that it sorts the elements (there is no "order" for the elements in the mathematical set).

Some STL implementations also support hash_set , which is very similar to set , since it is also an analog of the mathematical concept of sets. The big differences between set and hash_set are that hash_sets do not sort their elements, they have different performance characteristics (O (1), not O (log n), provided they have a good hash function), and, of course, they are not standard.

+3
source

What are they?

A lot is a collection.

A set is similar to a dictionary or โ€œmapโ€ of key / value pairs, except that it only stores (represents a set) of keys without corresponding values.

The set either runs or no instance of each possible key value. For example, a set of integers may contain the values โ€‹โ€‹{0, 1, 5}. A value (for example, 5) cannot be contained more than once in a set (if you call the set insert method more than once for a given key value, the set will still contain only one instance of this key value).

How are they useful?

I do not use them almost as often as cards.

One day I use a set if I am a library that allocates pointers that the client uses as a handle. I will save a personal set containing all the valid descriptor values โ€‹โ€‹that I created. When the client gives me the descriptor, I will check if the descriptor is a valid descriptor, checking if this value contains in my set.

+2
source

Quoting Wikipedia:

A set is a set of various objects considered as an object in their own right. Sets are one of the most fundamental concepts in mathematics. Although it was invented at the end of the 19th century, theory is now an ubiquitous part of mathematics, and can be used as a foundation from which almost all mathematics can be derived.

0
source

STL set red-black tree (at least the way I think it is implemented)

Another way to look at it.

Consequently, properties, quick search for elements, ordering of elements, uniqueness of elements, ordered traversal, etc.

This is useful when you want to keep track of unique elements, such as a list of unique strings or integers, but you can also store more complex structures.

0
source

For disordered set implementations in C ++ check out Boost.Unordered . In many cases, this is a better choice than the STL set, which I personally more or less use only to gradually create a sorted list.

0
source

All Articles