哪个std容器用于表示一堆扑克牌 [英] Which std container(s) to use to represent a stack of playing card

查看:112
本文介绍了哪个std容器用于表示一堆扑克牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我目前正在开展一个意味着堆叠扑克牌的项目。

我无法决定哪些容器用于表示它们。



以下是我可以列出的基本要求:



- 堆栈应顺序,因为卡片顺序很重要。 带我到序列容器。



- 堆栈的尺寸 未修复。卡可以从任意数量的随机位置取出,并且用于添加卡。 引导我到std :: list。



- 由于堆栈上的移动订单是从外部(用户)给出的,我需要检查堆栈是否包含卡(或堆栈的子集) 引导我使用关联容器,以便快速访问随机位置。



- 我们将能够随机播放堆栈



- 无法在堆栈中添加两次相同的卡。这些卡片是带有唯一ID的旗帜,所以从技术上讲,我可以在同一个堆叠中使用两个黑桃王,但不能使用相同的ID(如果你合并两张卡片用于播放)。



我知道没有一个独特的答案,但如果你有任何线索可以帮我决定扑克牌堆叠的最终结构那就太棒了!



我尝试过:



我已经提供了多种陈述。



首先,我用卡片的向量表示堆栈。

此解决方案适用于所有约束,但检查卡片或子集是否很慢堆栈中包含卡片。

在随机位置添加或移除卡片也很昂贵。



然后使用卡的列表组合 unordered_map

两者都通过添加和删除操作进行同步。

列表用于顺序,大小灵活ity和随机添加/删除。

unordered_map 用于随机访问(方法包含)。



然后,我意识到在列表中不可能使用std :: shuffle。所以暂时我使用的是 vector unordered_map 。回到随机添加/删除问题...

Hi everyone,

I am currently working on a project that implies stacks of playing cards.
And I have trouble deciding on which container(s) to use in order to represent them.

Here are the basic requirements that I can list:

- the stack shall be sequential because the cards order is important. Leads me to Sequence containers.

- the size of the stack is not fixed. Cards can be taken out from random places, in any number, and same for card adding. Leads me to std::list.

- As move orders on stacks are given from the outside (users), I need to check whether or not a card (or a subset of the stack) is contained by the stack. Leads me to Associative containers for fast access to a random place.

- We shall be able to shuffle the stack.

- It is not possible to add twice the same card in the stack. The cards are flag with unique ID's, so technically, I can have two Kings of Spades in the same stack, but not with the same ID (if you merge two card packs for playing).

I know there is no a unique answer, but if you have any clue to help me deciding the final structure of my playing card stack it would be great !

What I have tried:

I have already provide several representations.

First I represent the stack by a vector of cards.
This solution works for every constraints, but it is slow to check if a card or a subset of card is contained by the stack.
It is also expensive to add or remove a card in a random place.

I then used a combination of a list of card and an unordered_map.
Both are synchronized by the adding and removal operations.
The list is used for sequenciality, size flexibility and random adding/removal.
The unordered_map is used for random access (method contains).

Then, I realized that it is not possible to use std::shuffle on a list. So for the moment, I am using a vector and an unordered_map. Going back to the random adding/removal issue...

推荐答案

std :: vector应该是你的默认值,特别是对于少量的项目(我认为你最多有54张牌(13x4 + 2个玩家)。



列表可以轻松插入,但代价几乎所有其他内容。实际上,除非你是选择每张卡的真实愚蠢表示,你应该能够将整个堆栈放在你的CPU缓存中,所以即使将最多53张卡片移动到一个地方进行插入也没什么大不了的。



同样,搜索54个项目列表的成本可能不会很昂贵。



根据Stroustrup的建议:列出邪恶? [ ^ ]
std::vector should really be your default, especially for a small number of items (I presume you have at most 54 cards (13x4 + 2 jokers).

Lists have the ability to insert easily, but at the cost of almost everything else. Realistically, unless you are choosing a really stupid representation of each card, you should be able to have the whole stack in your CPU cache, so even shifting at most 53 cards by one place for insertion is no big deal.

Similarly, the cost of searching a list of 54 items is not likely to be expensive.

As recommended by Stroustrup: Are lists evil?[^]


这篇关于哪个std容器用于表示一堆扑克牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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