C++ 选择排序方法似乎没有任何作用

C++ 选择排序方法似乎没有任何作用,c++,arrays,class,sorting,selection-sort,C++,Arrays,Class,Sorting,Selection Sort,我正在处理一个甲板类,由一系列卡片对象组成。我必须为这个deck类编写的方法之一是sort方法,它应该按数字对卡片进行排序,以一组卡片的顺序结束:每套1张,每套2张,每套3张,依此类推。 有了这个卡片类 class card { public: int number; string suit; card() {number = 1;suit = "blank";} card(int n, string s) {number = n; suit = s;}

我正在处理一个甲板类,由一系列卡片对象组成。我必须为这个deck类编写的方法之一是sort方法,它应该按数字对卡片进行排序,以一组卡片的顺序结束:每套1张,每套2张,每套3张,依此类推。 有了这个卡片类

class card {
public:
    int number;
    string suit;
    card() {number = 1;suit = "blank";}
    card(int n, string s) {number = n; suit = s;}

    void printCard() {
        cout << number << " of " << suit << "s";
    }
};
考虑到我已经成功地为其他涉及数组和类的项目实现了选择排序,我不太清楚为什么数组似乎什么都没有发生。我在这里做的唯一一件新事情是对具有多个变量的对象数组进行排序,但我非常确定我正在比较sort()中每个卡片之间的适当变量

如果有人看到一个明显的错误,或者知道是什么导致了这个问题,请告诉我。这将大大有助于我解决这个问题


编辑:为deck类和调试输出中使用的其他方法添加代码。

您的问题在于选择
min

for(j=i+1;j<numCards;j++) {
    if(cardArray[i].number < cardArray[min].number)
        min = j;
}

请发布所有相关代码(例如,
swapCards
是如何定义的,以及什么是
numCards
)。另外,为什么不在每个排序迭代中添加调试输出?
if(cardArray[i].number
。我相当肯定你会想在这个比较中使用
j
。。。(另外,除非您在
swapCards
中做了一些非常特殊的事情,
std::swap
会很好地工作。)您有什么理由不能使用
std::sort
?@user657267排序方法是这个项目类的必需部分。就是这样!是的,按照循环的设置方式,我的意思是检查cardArray[j]与迄今为止的最低值。谢谢,我想我需要更仔细地检查一下我的代码。
deck d;

d.printDeck();
cout << endl;
d.shuffle();
d.printDeck();
cout << endl;
d.sort();
d.printDeck();
1 of hearts
2 of hearts
3 of hearts
4 of hearts
5 of hearts
6 of hearts
7 of hearts
8 of hearts
9 of hearts
10 of hearts
11 of hearts
12 of hearts
13 of hearts
1 of clubs
2 of clubs
3 of clubs
4 of clubs
5 of clubs
6 of clubs
7 of clubs
8 of clubs
9 of clubs
10 of clubs
11 of clubs
12 of clubs
13 of clubs
1 of spades
2 of spades
3 of spades
4 of spades
5 of spades
6 of spades
7 of spades
8 of spades
9 of spades
10 of spades
11 of spades
12 of spades
13 of spades
1 of diamonds
2 of diamonds
3 of diamonds
4 of diamonds
5 of diamonds
6 of diamonds
7 of diamonds
8 of diamonds
9 of diamonds
10 of diamonds
11 of diamonds
12 of diamonds
13 of diamonds

7 of diamonds
6 of spades
5 of diamonds
12 of clubs
2 of diamonds
5 of spades
8 of diamonds
4 of clubs
1 of hearts
8 of clubs
10 of hearts
13 of diamonds
11 of diamonds
12 of spades
2 of hearts
3 of spades
12 of diamonds
7 of spades
11 of hearts
5 of clubs
5 of hearts
1 of spades
10 of diamonds
7 of clubs
4 of diamonds
3 of diamonds
13 of spades
13 of hearts
12 of hearts
6 of clubs
1 of diamonds
11 of spades
9 of hearts
3 of hearts
9 of diamonds
9 of clubs
10 of clubs
10 of spades
9 of spades
2 of spades
6 of hearts
8 of spades
4 of hearts
1 of clubs
11 of clubs
4 of spades
3 of clubs
2 of clubs
6 of diamonds
8 of hearts
13 of clubs
7 of hearts

7 of diamonds
6 of spades
5 of diamonds
12 of clubs
2 of diamonds
5 of spades
8 of diamonds
4 of clubs
1 of hearts
8 of clubs
10 of hearts
13 of diamonds
11 of diamonds
12 of spades
2 of hearts
3 of spades
12 of diamonds
7 of spades
11 of hearts
5 of clubs
5 of hearts
1 of spades
10 of diamonds
7 of clubs
4 of diamonds
3 of diamonds
13 of spades
13 of hearts
12 of hearts
6 of clubs
1 of diamonds
11 of spades
9 of hearts
3 of hearts
9 of diamonds
9 of clubs
10 of clubs
10 of spades
9 of spades
2 of spades
6 of hearts
8 of spades
4 of hearts
1 of clubs
11 of clubs
4 of spades
3 of clubs
2 of clubs
6 of diamonds
8 of hearts
13 of clubs
7 of hearts
Press any key to continue . . .
for(j=i+1;j<numCards;j++) {
    if(cardArray[i].number < cardArray[min].number)
        min = j;
}
for(j=i+1;j<numCards;j++) {
    if(cardArray[j].number < cardArray[min].number)
        min = j;
}