本文介绍了我的C语言排序算法问题++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code在C ++

i have following code in c++

#include <iostream>
using namespace std;

void qsort5(int a[],int n){
    int i;
    int j;
    if (n<=1)
        return;
    for (i=1;i<n;i++)
        j=0;
    if (a[i]<a[0])
        swap(++j,i,a);
    swap(0,j,a);
    qsort5(a,j);
    qsort(a+j+1,n-j-1);
}

int main()
{
    return 0;
}

void swap(int i,int j,int a[])
{
    int t=a[i];
    a[i]=a[j];
    a[j]=t;
}

我有问题

1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(2203) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(76) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(13) : error C2780: 'void std::swap(_Ty &,_Ty &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(16) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(std::basic_string<_Elem,_Traits,_Alloc> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\xstring(2203) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(std::pair<_Ty1,_Ty2> &,std::pair<_Ty1,_Ty2> &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(76) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(14) : error C2780: 'void std::swap(_Ty &,_Ty &)' : expects 2 arguments - 3 provided
1>        c:\program files\microsoft visual studio 9.0\vc\include\utility(16) : see declaration of 'std::swap'
1>c:\users\dato\documents\visual studio 2008\projects\qsort5\qsort5\qsort5.cpp(16) : error C2661: 'qsort' : no overloaded function takes 2 arguments
1>Build log was saved at "file://c:\Users\dato\Documents\Visual Studio 2008\Projects\qsort5\qsort5\Debug\BuildLog.htm"

请帮忙

推荐答案

交换 STD A函数必须包括&LT;的iostream&GT; 。当您尝试打电话到您的交换,它无法找到它(我将在稍后解释),而是着眼于 STD ::排序,其中有两个参数(因此第一个错误)。

swap is a function in std which must be included by <iostream>. When you attempt to make calls to your swap, it can't find it (I'll explain in a moment) and instead looks at the std::sort, which takes two arguments (hence the first error).

无法找到你的交换的原因是因为它宣称它被调用后。你需要移动你的交换的定义上面的 qsort5 或转发声明它:

The reason it can't find your swap is because it is declared after it is called. You need to either move the definition of your swap above that of qsort5 or forward declare it:

void swap(int i,int j,int a[]);

void qsort5(int a[],int n){ 
  ...

这是告诉你的存在交换功能的编译器,它会使用,当你调用交换有3个参数。

That tells the compiler that your swap function exists and it will use that when you call swap with 3 arguments.

这篇关于我的C语言排序算法问题++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 16:59