I'm having a hard time conceptualizing c++ sets, actually sets in general.
我很难概念化c ++集,实际上是一般的。
What are they? How are they useful?
他们是什么?它们有用吗?
8 个解决方案
#1
25
Don't feel bad if you have trouble understanding sets in general. Most of a degree in mathematics is spent coming to terms with set theory:
如果您在理解集合时遇到困难,请不要感到难过。大多数数学学位都是用集合论来解决的:
http://en.wikipedia.org/wiki/Set_theory
Think of a set as a collection of unique, unordered objects. In many ways it looks like a list:
将集合视为唯一的无序对象的集合。在许多方面,它看起来像一个列表:
{ 1, 2, 3, 4 }
{1,2,3,4}
but order is unimportant:
但顺序并不重要:
{ 4, 3, 2, 1} = { 1, 2, 3, 4}
{4,3,2,1} = {1,2,3,4}
and repetitions are ignored:
重复被忽略:
{ 1, 1, 2, 3, 4 } = { 1, 2, 3, 4}
{1,1,2,3,4} = {1,2,3,4}
A C++ set is an implementation of this mathematical object, with the odd feature that is is sorted internally. But this is just a detail of implementation, and is not relevant to understanding the data structure. The sorting is just for speed.
C ++集是这个数学对象的一个实现,其奇怪的特性是在内部排序的。但这只是实现的细节,与理解数据结构无关。排序只是为了速度。
#2
5
C++ STL Sets are associative mappings that guarantee both sorting and uniqueness of elements within the set (Multisets guarantee the former but not the latter).
C ++ STL集是关联映射,它保证集合中元素的排序和唯一性(Multisets保证前者而不是后者)。
They are typically used as part of set operations - things like unions, intersections, and other interactions involving inclusion/exclusion of elements within a set.
它们通常用作集合操作的一部分 - 例如联合,交叉和其他涉及包含/排除集合中元素的交互。
#3
3
"Set" is a kind of collection that store multiple but unique objects. It is useful when you want to collect objects but you don't care their order or how many times same object are in it.
“Set”是一种存储多个但唯一的对象的集合。当您想要收集对象但不关心它们的顺序或者同一个对象在其中的次数时,它很有用。
See this for more detail: Set in C++
有关更多详细信息,请参阅此内容:使用C ++进
#4
3
Sets "in general" are a (very fundamental) concept in mathematics.
集“一般”是数学中的(非常基本的)概念。
STL's set
is based on the mathematical concept of a set: it's a collection of unique members, or a "Unique Associative Container" in STL terminology. The one slightly odd thing is that it sorts elements (in a mathematical set, there is no "order" to the elements).
STL的集合基于集合的数学概念:它是唯一成员的集合,或STL术语中的“唯一关联容器”。一个有点奇怪的是它对元素进行排序(在数学集中,元素没有“顺序”)。
Some STL implementations also support a hash_set
, which is very similar to set
, in that it is also an analog to the mathematical concept of a set. The big differences between set and hash_set
are that hash_sets do not sort their elements, they have different performance characteristics (O(1) rather than O(log n) look-ups, assuming a good hash function), and of course they aren't standard.
一些STL实现还支持hash_set,它与set非常相似,因为它也是一个集合的数学概念的类比。 set和hash_set之间的最大区别是hash_sets不对它们的元素进行排序,它们具有不同的性能特征(O(1)而不是O(log n)查找,假设一个好的哈希函数),当然它们不是'标准。
#5
2
What are they?
他们是什么?
A set is a collection.
集合是一个集合。
A set is like a dictionary or 'map' of key/value pairs, except that it only stores (is a collection of) keys without associated values.
集合类似于字典或键/值对的“映射”,除了它只存储(是一组)没有关联值的键。
A set either does or doesn't contain an instance of each possible key value. For example, a set of integers might contain the values {0, 1, 5}. A value (e.g. 5) can't be contained more than once in the set (if you call the set's insert method more than once for a given key value, the set will still contain only one instance of that key value).
集合包含或不包含每个可能键值的实例。例如,一组整数可能包含值{0,1,5}。一个值(例如5)不能在集合中包含多于一次(如果对给定的键值多次调用set的insert方法,该集合仍将只包含该键值的一个实例)。
How are they useful?
它们有用吗?
I don't use them nearly as often as maps.
我几乎不像地图那样经常使用它们。
One time I use a set is if I'm a library which gives away pointers which a client uses as a handle. I'll keep a private set which contains all the valid handle values which I've created. When the client gives me a handle, I'll test whether the handle is a valid handle by testing whether that value is contained in my set.
有一次我使用一个集合,如果我是一个库,它提供客户端用作句柄的指针。我将保留一个包含我创建的所有有效句柄值的私有集。当客户端给我一个句柄时,我将通过测试该值是否包含在我的集合中来测试句柄是否是有效的句柄。
#6
0
A set is a collection of distinct objects, considered as an object in its own right. Sets are one of the most fundamental concepts in mathematics. Although it was invented at the end of the 19th century, set theory is now a ubiquitous part of mathematics, and can be used as a foundation from which nearly all of mathematics can be derived.
集合是不同对象的集合,本身被视为对象。集是数学中最基本的概念之一。虽然它是在19世纪末发明的,但是集合论现在已经成为数学中无处不在的一部分,并且可以作为几乎所有数学都可以得出的基础。
#7
0
STL set
is a red-black tree (at least that's how I think it is implemented)
STL集是一棵红黑树(至少我认为它是如何实现的)
Another way to look at it.
另一种看待它的方式。
Hence the properties, fast element search, element ordering, element uniqueness, ordered traversal and so on.
因此,属性,快速元素搜索,元素排序,元素唯一性,有序遍历等。
It is useful when you want to keep track of unique elements like for example list of unique strings or integers but you can store more complicated structures as well.
当您想要跟踪唯一元素(例如唯一字符串或整数列表)时,它非常有用,但您也可以存储更复杂的结构。
#8
0
For an unordered implementation of sets in C++, check out Boost.Unordered. In many cases this is a better choice than STL set, which I personally more or less use only to build a sorted list incrementally.
对于C ++中无序的集合实现,请查看Boost.Unordered。在许多情况下,这是一个比STL集合更好的选择,我个人或多或少只使用它来逐步构建排序列表。
#1
25
Don't feel bad if you have trouble understanding sets in general. Most of a degree in mathematics is spent coming to terms with set theory:
如果您在理解集合时遇到困难,请不要感到难过。大多数数学学位都是用集合论来解决的:
http://en.wikipedia.org/wiki/Set_theory
Think of a set as a collection of unique, unordered objects. In many ways it looks like a list:
将集合视为唯一的无序对象的集合。在许多方面,它看起来像一个列表:
{ 1, 2, 3, 4 }
{1,2,3,4}
but order is unimportant:
但顺序并不重要:
{ 4, 3, 2, 1} = { 1, 2, 3, 4}
{4,3,2,1} = {1,2,3,4}
and repetitions are ignored:
重复被忽略:
{ 1, 1, 2, 3, 4 } = { 1, 2, 3, 4}
{1,1,2,3,4} = {1,2,3,4}
A C++ set is an implementation of this mathematical object, with the odd feature that is is sorted internally. But this is just a detail of implementation, and is not relevant to understanding the data structure. The sorting is just for speed.
C ++集是这个数学对象的一个实现,其奇怪的特性是在内部排序的。但这只是实现的细节,与理解数据结构无关。排序只是为了速度。
#2
5
C++ STL Sets are associative mappings that guarantee both sorting and uniqueness of elements within the set (Multisets guarantee the former but not the latter).
C ++ STL集是关联映射,它保证集合中元素的排序和唯一性(Multisets保证前者而不是后者)。
They are typically used as part of set operations - things like unions, intersections, and other interactions involving inclusion/exclusion of elements within a set.
它们通常用作集合操作的一部分 - 例如联合,交叉和其他涉及包含/排除集合中元素的交互。
#3
3
"Set" is a kind of collection that store multiple but unique objects. It is useful when you want to collect objects but you don't care their order or how many times same object are in it.
“Set”是一种存储多个但唯一的对象的集合。当您想要收集对象但不关心它们的顺序或者同一个对象在其中的次数时,它很有用。
See this for more detail: Set in C++
有关更多详细信息,请参阅此内容:使用C ++进
#4
3
Sets "in general" are a (very fundamental) concept in mathematics.
集“一般”是数学中的(非常基本的)概念。
STL's set
is based on the mathematical concept of a set: it's a collection of unique members, or a "Unique Associative Container" in STL terminology. The one slightly odd thing is that it sorts elements (in a mathematical set, there is no "order" to the elements).
STL的集合基于集合的数学概念:它是唯一成员的集合,或STL术语中的“唯一关联容器”。一个有点奇怪的是它对元素进行排序(在数学集中,元素没有“顺序”)。
Some STL implementations also support a hash_set
, which is very similar to set
, in that it is also an analog to the mathematical concept of a set. The big differences between set and hash_set
are that hash_sets do not sort their elements, they have different performance characteristics (O(1) rather than O(log n) look-ups, assuming a good hash function), and of course they aren't standard.
一些STL实现还支持hash_set,它与set非常相似,因为它也是一个集合的数学概念的类比。 set和hash_set之间的最大区别是hash_sets不对它们的元素进行排序,它们具有不同的性能特征(O(1)而不是O(log n)查找,假设一个好的哈希函数),当然它们不是'标准。
#5
2
What are they?
他们是什么?
A set is a collection.
集合是一个集合。
A set is like a dictionary or 'map' of key/value pairs, except that it only stores (is a collection of) keys without associated values.
集合类似于字典或键/值对的“映射”,除了它只存储(是一组)没有关联值的键。
A set either does or doesn't contain an instance of each possible key value. For example, a set of integers might contain the values {0, 1, 5}. A value (e.g. 5) can't be contained more than once in the set (if you call the set's insert method more than once for a given key value, the set will still contain only one instance of that key value).
集合包含或不包含每个可能键值的实例。例如,一组整数可能包含值{0,1,5}。一个值(例如5)不能在集合中包含多于一次(如果对给定的键值多次调用set的insert方法,该集合仍将只包含该键值的一个实例)。
How are they useful?
它们有用吗?
I don't use them nearly as often as maps.
我几乎不像地图那样经常使用它们。
One time I use a set is if I'm a library which gives away pointers which a client uses as a handle. I'll keep a private set which contains all the valid handle values which I've created. When the client gives me a handle, I'll test whether the handle is a valid handle by testing whether that value is contained in my set.
有一次我使用一个集合,如果我是一个库,它提供客户端用作句柄的指针。我将保留一个包含我创建的所有有效句柄值的私有集。当客户端给我一个句柄时,我将通过测试该值是否包含在我的集合中来测试句柄是否是有效的句柄。
#6
0
A set is a collection of distinct objects, considered as an object in its own right. Sets are one of the most fundamental concepts in mathematics. Although it was invented at the end of the 19th century, set theory is now a ubiquitous part of mathematics, and can be used as a foundation from which nearly all of mathematics can be derived.
集合是不同对象的集合,本身被视为对象。集是数学中最基本的概念之一。虽然它是在19世纪末发明的,但是集合论现在已经成为数学中无处不在的一部分,并且可以作为几乎所有数学都可以得出的基础。
#7
0
STL set
is a red-black tree (at least that's how I think it is implemented)
STL集是一棵红黑树(至少我认为它是如何实现的)
Another way to look at it.
另一种看待它的方式。
Hence the properties, fast element search, element ordering, element uniqueness, ordered traversal and so on.
因此,属性,快速元素搜索,元素排序,元素唯一性,有序遍历等。
It is useful when you want to keep track of unique elements like for example list of unique strings or integers but you can store more complicated structures as well.
当您想要跟踪唯一元素(例如唯一字符串或整数列表)时,它非常有用,但您也可以存储更复杂的结构。
#8
0
For an unordered implementation of sets in C++, check out Boost.Unordered. In many cases this is a better choice than STL set, which I personally more or less use only to build a sorted list incrementally.
对于C ++中无序的集合实现,请查看Boost.Unordered。在许多情况下,这是一个比STL集合更好的选择,我个人或多或少只使用它来逐步构建排序列表。