std :: set和std :: vector有什么区别?

时间:2022-05-10 13:25:10

I am learning STL now. I read about set container. I have question when you want to use set? After reading description of set it looks like it is useless because we can substitute it by vector. Could you say pros and cos for vector vs set containers. Thanks

我现在正在学习STL。我读到了关于set container的内容。我有问题什么时候你想使用套装?在阅读了集合的描述后,看起来它没用,因为我们可以用矢量代替它。你能说矢量与集合容器的优缺点吗?谢谢

4 个解决方案

#1


78  

A set is ordered. It is guaranteed to remain in a specific ordering, according to a functor that you provide. No matter what elements you add or remove (unless you add a duplicate, which is not allowed in a set), it will always be ordered.

订购了一套。根据您提供的仿函数,它保证保持特定的顺序。无论您添加或删除哪些元素(除非您添加一个副本,在集合中不允许),它将始终被订购。

A vector has exactly and only the ordering you explicitly give it. Items in a vector are where you put them. If you put them in out of order, then they're out of order; you now need to sort the container to put them back in order.

向量具有完全且仅有您明确给出的顺序。矢量中的项目是您放置它们的位置。如果你把它们排除在外,那么它们就会失灵;您现在需要对容器进行排序以使它们按顺序排列。

Admittedly, set has relatively limited use. With proper discipline, one could insert items into a vector and keep it ordered. However, if you are constantly inserting and removing items from the container, vector will run into many issues. It will be doing a lot of copying/moving of elements and so forth, since it is effectively just an array.

不可否认,套装的使用相对有限。通过适当的纪律,人们可以将项目插入向量并保持有序。但是,如果您不断地从容器中插入和移除项目,向量将遇到许多问题。它将进行大量复制/移动元素等等,因为它实际上只是一个数组。

The time it takes to insert an item into a vector is proportional to the number of items already in the vector. The time it takes to insert an item into a set is proportional to the log₂ of the number of items. If the number of items is large, that's a huge difference. log₂(100,000) is ~16; that's a major speed improvement. The same goes for removal.

将项目插入向量所需的时间与向量中已有的项目数成正比。将项目插入集合所需的时间与项目数量的log 2成比例。如果项目数量很大,那将是一个巨大的差异。 log 2(100,000)是~16;这是一个主要的速度提升。移除也是如此。

However, if you do all of your insertions at once, at initialization time, then there's no problem. You can insert everything into the vector, sort it (paying that price once), and then use standard algorithms for sorted vectors to find elements and iterate over the sorted list. And while iteration over the elements of a set isn't exactly slow, iterating over a vector is faster.

但是,如果您在初始化时一次完成所有插入操作,那么就没有问题。您可以将所有内容插入到矢量中,对其进行排序(支付该价格一次),然后对已排序的矢量使用标准算法来查找元素并迭代排序列表。虽然对集合元素的迭代不是很慢,但迭代向量的速度更快。

So there are cases where a sorted vector beats a set. That being said, you really shouldn't bother with the expense of this kind of optimization unless you know that it is necessary. So use a set unless you have experience with the kind of system you're writing (and thus know that you need that performance) or have profiling data in hand that tells you that you need a vector and not a set.

因此,存在排序向量胜过集合的情况。话虽这么说,除非你知道这是必要的,否则你真的不应该为这种优化付出代价。因此,除非您对所编写的系统有所了解(因此知道您需要该性能),或者手头的分析数据告诉您需要向量而不是集合,否则请使用集合。

#2


8  

They are different things: you decide how vectors are ordered, and you can also put as many equal things into a vector as you please. Sets are ordered in accordance to that set's internal rules (you may set the rules, but the set will deal with the ordering), and you cannot put multiple equal items into a set.

它们是不同的东西:你决定如何排序矢量,你也可以根据需要将尽可能多的相同的东西放入矢量中。集合是根据该集合的内部规则排序的(您可以设置规则,但集合将处理排序),并且您不能将多个相等的项目放入集合中。

Of course you could maintain a vector of unique items, but your performance would suffer a lot when you do set-oriented operations. For example, assume that you have a set of 10000 items and a vector of 10000 distinct unordered items. Now suppose that you need to check if a value X is among the values in the set (or among the values in the vector). When X is not among the items, searching the vector would be some 100 times slower. You would see similar performance differences on calculating set unions and intersections.

当然,您可以维护一个独特项目的向量,但是当您进行面向集合的操作时,您的性能会受到很大影响。例如,假设您有一组10000个项目和10000个不同的无序项目的向量。现在假设您需要检查值X是否在集合中的值(或向量中的值之间)。当X不在项目中时,搜索向量将慢大约100倍。您会在计算集合联合和交叉点时看到类似的性能差异。

To summarize, sets and vectors have different purposes. You can use a vector instead of a set, but it would require more work, and would likely hurt the performance rather severely.

总而言之,集合和向量具有不同的目的。你可以使用向量而不是集合,但它需要更多的工作,并且可能会严重损害性能。

#3


5  

it is faster to search an item against a set than a vector (O(log(n)) vs O(n)). To search an item against a vector, you need to iterate all items in the vector, but the set use red-black tree to optimize the search, only a few item will be looked to find a match.

根据集合而不是向量搜索项目(O(log(n))vs O(n))。要针对向量搜索项目,您需要迭代向量中的所有项目,但是该集合使用红黑树来优化搜索,只有少数项目将被查找以找到匹配项。

The set is ordered, it means you can only iterate it from smallest one to biggest one by order, or the reversed order.

该集合是有序的,这意味着您只能按顺序将其从最小的一个迭代到最大的一个,或者相反的顺序。

But the vector is unordered, you can travel it by the insert order.

但是矢量是无序的,您可以通过插入顺序来移动它。

#4


2  

form cpluplus.com set:

表格cpluplus.com设置:

Sets are containers that store unique elements following a specific order.

集合是按特定顺序存储唯一元素的容器。

so the set is ordered AND item are uniquely represented

所以集合是有序的,并且项目是唯一表示的

while vect:

而vect:

Vectors are sequence containers representing arrays that can change in size.

向量是表示可以改变大小的数组的序列容器。

so vector is in the order you fill it AND can hold multiple identical items

所以矢量按你填充的顺序排列,并且可以容纳多个相同的项目

prefer set:

偏好集:

  • if you wish to filter multiple identical values
  • 如果您希望过滤多个相同的值
  • if you wish to parse items in a specified order (doing this in vector requires to specifically sort vector).
  • 如果您希望按指定顺序解析项目(在向量中执行此操作需要专门对向量进行排序)。

prefer vector:

喜欢矢量:

  • if you want to keep identical values
  • 如果你想保持相同的价值观
  • if you wish to parse items in same order as you pushed them (assuming you don't process the vector order)
  • 如果您希望以推送它们的顺序解析项目(假设您不处理矢量顺序)

#1


78  

A set is ordered. It is guaranteed to remain in a specific ordering, according to a functor that you provide. No matter what elements you add or remove (unless you add a duplicate, which is not allowed in a set), it will always be ordered.

订购了一套。根据您提供的仿函数,它保证保持特定的顺序。无论您添加或删除哪些元素(除非您添加一个副本,在集合中不允许),它将始终被订购。

A vector has exactly and only the ordering you explicitly give it. Items in a vector are where you put them. If you put them in out of order, then they're out of order; you now need to sort the container to put them back in order.

向量具有完全且仅有您明确给出的顺序。矢量中的项目是您放置它们的位置。如果你把它们排除在外,那么它们就会失灵;您现在需要对容器进行排序以使它们按顺序排列。

Admittedly, set has relatively limited use. With proper discipline, one could insert items into a vector and keep it ordered. However, if you are constantly inserting and removing items from the container, vector will run into many issues. It will be doing a lot of copying/moving of elements and so forth, since it is effectively just an array.

不可否认,套装的使用相对有限。通过适当的纪律,人们可以将项目插入向量并保持有序。但是,如果您不断地从容器中插入和移除项目,向量将遇到许多问题。它将进行大量复制/移动元素等等,因为它实际上只是一个数组。

The time it takes to insert an item into a vector is proportional to the number of items already in the vector. The time it takes to insert an item into a set is proportional to the log₂ of the number of items. If the number of items is large, that's a huge difference. log₂(100,000) is ~16; that's a major speed improvement. The same goes for removal.

将项目插入向量所需的时间与向量中已有的项目数成正比。将项目插入集合所需的时间与项目数量的log 2成比例。如果项目数量很大,那将是一个巨大的差异。 log 2(100,000)是~16;这是一个主要的速度提升。移除也是如此。

However, if you do all of your insertions at once, at initialization time, then there's no problem. You can insert everything into the vector, sort it (paying that price once), and then use standard algorithms for sorted vectors to find elements and iterate over the sorted list. And while iteration over the elements of a set isn't exactly slow, iterating over a vector is faster.

但是,如果您在初始化时一次完成所有插入操作,那么就没有问题。您可以将所有内容插入到矢量中,对其进行排序(支付该价格一次),然后对已排序的矢量使用标准算法来查找元素并迭代排序列表。虽然对集合元素的迭代不是很慢,但迭代向量的速度更快。

So there are cases where a sorted vector beats a set. That being said, you really shouldn't bother with the expense of this kind of optimization unless you know that it is necessary. So use a set unless you have experience with the kind of system you're writing (and thus know that you need that performance) or have profiling data in hand that tells you that you need a vector and not a set.

因此,存在排序向量胜过集合的情况。话虽这么说,除非你知道这是必要的,否则你真的不应该为这种优化付出代价。因此,除非您对所编写的系统有所了解(因此知道您需要该性能),或者手头的分析数据告诉您需要向量而不是集合,否则请使用集合。

#2


8  

They are different things: you decide how vectors are ordered, and you can also put as many equal things into a vector as you please. Sets are ordered in accordance to that set's internal rules (you may set the rules, but the set will deal with the ordering), and you cannot put multiple equal items into a set.

它们是不同的东西:你决定如何排序矢量,你也可以根据需要将尽可能多的相同的东西放入矢量中。集合是根据该集合的内部规则排序的(您可以设置规则,但集合将处理排序),并且您不能将多个相等的项目放入集合中。

Of course you could maintain a vector of unique items, but your performance would suffer a lot when you do set-oriented operations. For example, assume that you have a set of 10000 items and a vector of 10000 distinct unordered items. Now suppose that you need to check if a value X is among the values in the set (or among the values in the vector). When X is not among the items, searching the vector would be some 100 times slower. You would see similar performance differences on calculating set unions and intersections.

当然,您可以维护一个独特项目的向量,但是当您进行面向集合的操作时,您的性能会受到很大影响。例如,假设您有一组10000个项目和10000个不同的无序项目的向量。现在假设您需要检查值X是否在集合中的值(或向量中的值之间)。当X不在项目中时,搜索向量将慢大约100倍。您会在计算集合联合和交叉点时看到类似的性能差异。

To summarize, sets and vectors have different purposes. You can use a vector instead of a set, but it would require more work, and would likely hurt the performance rather severely.

总而言之,集合和向量具有不同的目的。你可以使用向量而不是集合,但它需要更多的工作,并且可能会严重损害性能。

#3


5  

it is faster to search an item against a set than a vector (O(log(n)) vs O(n)). To search an item against a vector, you need to iterate all items in the vector, but the set use red-black tree to optimize the search, only a few item will be looked to find a match.

根据集合而不是向量搜索项目(O(log(n))vs O(n))。要针对向量搜索项目,您需要迭代向量中的所有项目,但是该集合使用红黑树来优化搜索,只有少数项目将被查找以找到匹配项。

The set is ordered, it means you can only iterate it from smallest one to biggest one by order, or the reversed order.

该集合是有序的,这意味着您只能按顺序将其从最小的一个迭代到最大的一个,或者相反的顺序。

But the vector is unordered, you can travel it by the insert order.

但是矢量是无序的,您可以通过插入顺序来移动它。

#4


2  

form cpluplus.com set:

表格cpluplus.com设置:

Sets are containers that store unique elements following a specific order.

集合是按特定顺序存储唯一元素的容器。

so the set is ordered AND item are uniquely represented

所以集合是有序的,并且项目是唯一表示的

while vect:

而vect:

Vectors are sequence containers representing arrays that can change in size.

向量是表示可以改变大小的数组的序列容器。

so vector is in the order you fill it AND can hold multiple identical items

所以矢量按你填充的顺序排列,并且可以容纳多个相同的项目

prefer set:

偏好集:

  • if you wish to filter multiple identical values
  • 如果您希望过滤多个相同的值
  • if you wish to parse items in a specified order (doing this in vector requires to specifically sort vector).
  • 如果您希望按指定顺序解析项目(在向量中执行此操作需要专门对向量进行排序)。

prefer vector:

喜欢矢量:

  • if you want to keep identical values
  • 如果你想保持相同的价值观
  • if you wish to parse items in same order as you pushed them (assuming you don't process the vector order)
  • 如果您希望以推送它们的顺序解析项目(假设您不处理矢量顺序)