这是我正在尝试的简单冒泡排序:
template<class T>
void bubbleSort(T *begin, T *end) {
for (auto index = begin + 1; index != end; ++index) {
for (auto bubble = begin; bubble != end - 1; ++bubble) {
if (*bubble > *(bubble + 1)) {
const T temp = *bubble;
*bubble = *(bubble + 1);
*(bubble + 1) = temp;
}
}
}
}
这个版本似乎有效(在其所有冒泡排序的荣耀中)。顺便说一句,如果有帮助,这是我正在测试的课程:
class Numbers {
int max;
int *numbers;
public:
Numbers(initializer_list<int> initialList) : max { initialList.size() }, numbers { new int[max] }
{
int index = 0;
for (auto it = initialList.begin(); it != initialList.end(); ++it, ++index) {
numbers[index] = *it;
}
}
int operator *(int index) { return numbers[index]; }
int *begin() { return &numbers[0]; }
int *end() { return &numbers[max]; }
};
我试图做的是在我的内部循环中使用std::swap
如下方式编写手动交换:
for (auto bubble = begin; bubble != end - 1; ++bubble) {
if (*bubble > *(bubble + 1)) swap (bubble, bubble + 1);
}
但由于某种原因,编译器告诉我:
error C2665: 'std::swap' : none of the 3 overloads could convert all the argument types
这是为什么?