2d 排序卡算法
2d Sorting card algorithm
我正在尝试为一手牌制作一种排序算法,它应该按类型排序,然后按 num 排序,所以输出看起来像 1 俱乐部 5个俱乐部 7 俱乐部 3 钻石 10 钻石 钻石之王等 但我只能让它按花色排序,输出更像 5个俱乐部 1 俱乐部 7 俱乐部 3 钻石 钻石之王 10 钻石等 有谁知道我该怎么做?
void player::handsort() {
int hold[2][13], count=0;
for (int i = 0; i < 13; ++i) {
if (type[i]==Clubs){
hold[1][count]=type[i];
hold[0][count]=hand[i];
count++;
}
}
for (int i = 0; i < 13; ++i) {
if (type[i]==Diamonds){
hold[1][count]=type[i];
hold[0][count]=hand[i];
count++;
}
}
for (int i = 0; i < 13; ++i) {
if (type[i]==Hearts){
hold[1][count]=type[i];
hold[0][count]=hand[i];
count++;
}
}
for (int i = 0; i < 13; ++i) {
if (type[i]==Spades){
hold[1][count]=type[i];
hold[0][count]=hand[i];
count++;
}
}
for (int i = 0; i < 13; ++i) {
hand[i]=hold[0][i];
type[i]=hold[1][i];
}
}
我会考虑将您的实现更改为更模块化的东西。
enum Suit { CLUB, DIAMOND, HEARTS, SPADES };
struct Card {
Suit suit;
int num;
bool operator<(Card c) const {
if (suit != c.suit)
return suit < c.suit;
return num < c.num;
}
};
void handsort(std::vector<Card> &hand) { std::sort(hand.begin(), hand.end()); }
请记住,这是C++。利用自由抽象!
相关文章:
- C++选择排序算法中的逻辑错误
- 排序算法c++
- 下面是排序算法O(n)吗
- 为什么我的排序算法会更改数组值
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 排序算法问题(购买商店物品)(崩溃)
- 为什么这个选择排序算法仍然切换一个元素,当它已经是其他元素中最小的元素时?
- 为什么在此排序算法实现中,向量明显比数组慢?
- C++ 多线程 - 与线程合并排序的算法替代
- C++中合并排序算法的奇怪行为
- 如果要求比较器是严格的总排序,而不仅仅是严格的弱排序,C++标准算法会更快吗?
- 我的排序算法程序中的堆错误
- 运行合并排序递归算法时EXC_BAD_ACCESS错误
- 如何使用cpp编写选择排序算法以降序对元素列表进行排序?
- 解释一下这个排序算法是如何工作的?
- 检查向量是否使用除法和阻抗算法进行排序
- 使用sort()算法C 排序对象列表
- 算法排序1101001011
- 克鲁斯卡尔算法(排序)
- 用STL算法排序结构的动态数组