为什么我的代码可以运行,结果却不对呢

为什么我这段代码可以运行,结果却不对呢

为什么我这段代码可以运行,结果却不对呢
#include
using namespace std;
template <typename T>
void mySwap(T a,T b)
{
    T temp = a;
    a = b;
    b = temp;
}
void sort(int *a, unsigned len, bool bigger=true )
{
    for(int i=1;ifor (int i = 1; i < len; i++)
        {
            bool bcase = bigger ? a[i] > a[i - 1]:a[i] < a[i - 1];
            if (bcase) mySwap(a[i],a[i-1]);
        }
}
int main()
{
    int a[5]{ 1,8,3,4,5 };
    sort(a, 5);
    for (auto x : a)std::cout << x << std::endl;
    system("pause");
    return 0;
}


有么有人知道,能告诉我吗非常感谢


for(int i=1;i<len;i++)
        for (int i = 1; i < len; i++)

循环变量不能两层都用i 啊
mySwap用值参是不能实现传递变量值交换的

你是在c++ 11以后的环境吗? for(auto X:a) 是需要定义向量的。
例子:

#include <vector>
...
   std::vector<int> v = {0, 1, 2, 3, 4, 5};

而且第22行的赋值缺少‘=’。