对一副卡片进行冒泡排序
我已经把自己带进了一个我不知道如何摆脱的兔子洞,我创建了一副随机卡片。但是,我现在需要首先按花色对它们进行排序,然后按枚举的顺序按排名对它们进行排序。首先,我不知道如何从向量内和枚举内获取元素,所以我可以比较它们,其次,我怀疑这个链接到第一个我不知道如何交换它们,最后会很好有一个用于冒泡排序的函数,我可以调用两次以按花色对它们进行排序,然后再次调用以按卡排序。
这是库:
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
using std::vector;
这些是结构和枚举:
enum Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }; // aspects of the card
enum Suit { HEARTS, CLUBS, DIAMONDS, SPADES }; // suits of the card
struct Card { //Card data numbers
Rank rank;
Suit suit;
};
struct Deck { // defines a card
vector<Card> cards; // All the cards
int size = 5; // Max deck size
};
这是生成随机卡片组的内容:
void initialize(Deck& deck, string stdno) {
for (int count=0; count < deck.size; count++) { //creates a deck of cards
Card card;
string rcardstr = stdno.substr(rand() % 8, 1) + stdno.substr(rand() % 8, 1);
int rcard = stoi(rcardstr) % 52;
card.suit = static_cast<Suit>(rcard / 13); //cast between the enumerator and the value
card.rank = static_cast<Rank>(1 + rcard % 13);
deck.cards.push_back(card); //Adds the card to the deck of cards
}
}
这是我在意识到前两个问题之前对冒泡排序的微弱尝试:
void bubble_sort(Deck& deck ){
bool swapp = true;
while (swapp) {
swapp = false;
for (int i=0; i < deck.size; i++) {
if (deck.rank[0]
}
}
}
...最后是我的主要功能:
int main() {
string stdno="14398132";
Deck my_deck;
initialize(my_deck, stdno);
}
如果有人愿意愿意给我几分钟的时间来解释我需要看哪里/我需要学习什么,我将非常感激,并确保在我在 c++ 方面变得更好后将同样的时间回馈给社区。
I have taken myself down a rabbit hole that I do not know how to get out of, I have created a deck of random cards. However, I now need to sort them firstly by suit then by rank in the order of my enum's. Firstly I can't figure out how to get elements from within the vector and within the enum, so I can compare them, secondly, I suspect this links to the first I'm not sure how to swap them, finally it would be nice to have a function for the bubble sort that I can call twice to first-order them by suit and then call again to order by card.
Here are the libraries:
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
using std::vector;
These are the structs and enum's:
enum Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }; // aspects of the card
enum Suit { HEARTS, CLUBS, DIAMONDS, SPADES }; // suits of the card
struct Card { //Card data numbers
Rank rank;
Suit suit;
};
struct Deck { // defines a card
vector<Card> cards; // All the cards
int size = 5; // Max deck size
};
This is what generates a random card deck:
void initialize(Deck& deck, string stdno) {
for (int count=0; count < deck.size; count++) { //creates a deck of cards
Card card;
string rcardstr = stdno.substr(rand() % 8, 1) + stdno.substr(rand() % 8, 1);
int rcard = stoi(rcardstr) % 52;
card.suit = static_cast<Suit>(rcard / 13); //cast between the enumerator and the value
card.rank = static_cast<Rank>(1 + rcard % 13);
deck.cards.push_back(card); //Adds the card to the deck of cards
}
}
This is my feeble attempts of a bubble sort before I realized my first two problems:
void bubble_sort(Deck& deck ){
bool swapp = true;
while (swapp) {
swapp = false;
for (int i=0; i < deck.size; i++) {
if (deck.rank[0]
}
}
}
...And finally my main function:
int main() {
string stdno="14398132";
Deck my_deck;
initialize(my_deck, stdno);
}
If someone would be willing to give me a few minutes of their time to explain where I need to look/what I need to learn I would be incredibly grateful and make sure to give the same time back to the community once I've become better at c++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
访问牌组成员的方法是这样写:
或
您有多种排序选项。
编写一个冒泡排序函数,但首先按花色比较每张牌,如果它们相等,则按排名比较它们,或者只是重载运算符
按照评论中的建议,选择您的卡片类别。这可能是首选方法并且更有效。
如果您不坚持使用冒泡排序,则可以使用
std::sort
和 lambda 函数,该函数首先按花色比较您的牌,然后按排名进行比较。对于交换,您应该能够使用 std::swap 或编写一个简单的自己的模板函数:
The way to access the members of the deck is to write:
or
You have multiple options for sorting.
and
Write one bubble sort function, but take the sort criteria as a parameter (which can be enum).
Write a single bubble sort function, but compare each card first by suit and if they were equal, then compare them by rank or just overload the operator< for your Card class as suggested in the comments. This is probably the preferred method and more efficient.
If you don't insist on using bubble sort, you can just use
std::sort
with a lambda function that compares your cards first by suit then by rank.For swapping you should be able to use std::swap or write a simple template function yourself: