为什么“设置"数据结构被告知是无序的? [英] Why 'set' data structure is told to be unordered?

查看:58
本文介绍了为什么“设置"数据结构被告知是无序的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我认为它是完全有序的,因为元素的位置取决于它们的哈希函数,因此,如果对象是不可变的,那么在将它们放入集合后,它们的位置将保持不变.而且,每当您打印集时,您将拥有完全相同的元素顺序.当然,在计算出哈希函数之前,您无法预测它们的位置,但是无论如何.

Well, I think it's perfectly ordered, because position of the elements depends on their hash function, thus if that objects are immutable, then after you put them in the set, their position will remain unchanged. And everytime you, say, print your set you'll have exact the same elements order. Sure, you can't predict their position until their hash function is calculated, but anyway.

推荐答案

通用/抽象数据类型

来自 Aho,Hopcroft,Ullmann set 的定义:数据结构和算法",Addison-Wesely,1987:

Generic / abstract data type

The definition of set from Aho, Hopcroft, Ullmann: 'Data Structures and Algorithms', Addison-Wesely, 1987:

集合是成员(或元素)的集合;[...].集合中的所有成员都不同,[...]

A set is a collection of members (or elements); [...] . All members of a set are different, [...]

抽象数据类型 set 不具有 ordered unordered 的特征.定义了一些对集合进行操作的方法-但它们都与排序无关(例如,参见 Martin Richards :数据结构和算法").

The abstract data type set does not have the characteristic of ordered or unordered. There are some methods defined which operate on a set - but none of them has something to do with ordering (e.g see Martin Richards: 'Data Structures and Algorithms').

如果一组中的每个元素也位于另一组中-并且没有其他元素,则这两个组是相等的.

Two sets are seen equal, if each element from one set is also inside the other - and there are no additional elements.

写下集合(以及集合的所有元素)时,需要以某种顺序写下来.请注意,这只是适当集合的表示.

When you write down a set (and therefore all the elements of a set) you need to write them down in some order. Note that this is just a representation of the appropriate set.

示例:包含元素1、2和3的集合可以记为{1、2、3},{1、3、2},{3、1、2}等.这些只是同一组的不同表示形式.

Example: A set which contains the elements one, two and three can be written down as {1, 2, 3}, {1, 3, 2}, {3, 1, 2} and so on. These are only different representations of the same set.

在不同的编程语言中,集合是通过不同的方式实现的,并考虑了不同的用例.

In different programming languages sets are implemented in different ways with different use cases in mind.

在某些语言(例如python,JAVA)中,标准集合实现不会在其接口中公开顺序.

In some languages (like python, JAVA) the standard set implementations do not expose ordering in their interfaces.

在某些语言(例如C ++)中,标准集合实现在其接口​​中公开排序.

In some languages (like C++) the standard set implementation exposes ordering in their interfaces.

示例(C ++):

在内部,集合中的元素总是按照针对容器构造的特定严格的弱排序准则集从低到高排序.

Internally, the elements in a set are always sorted from lower to higher following a specific strict weak ordering criterion set on container construction.

模板<类Key,类Compare =更少,class Allocator =分配器>类集;

template < class Key, class Compare = less, class Allocator = allocator > class set;

(请参阅 C ++集).

这篇关于为什么“设置"数据结构被告知是无序的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆