C++博客-linxuex-最新评论http://www.cppblog.com/linxuex/CommentsRSS.aspxC++学习者zh-cnSat, 21 Apr 2007 01:39:04 GMTSat, 21 Apr 2007 01:39:04 GMTcnblogsre: 请 教http://www.cppblog.com/linxuex/archive/2007/04/28/22451.html#23138pengkunypengkunySat, 28 Apr 2007 12:59:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/28/22451.html#23138正解如下:
for(i=0; i<n; i++)//排序
{
for(j=0; j<n-1-i; j++)
{
if(x[j] > x[j+1])
{
swap(x[j],x[j+1]);//标准库函数
}
}
}

pengkuny 2007-04-28 20:59 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/27/22451.html#23021sandysandyFri, 27 Apr 2007 03:03:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/27/22451.html#23021for(i=j;i<5-j;i++)
if(a[i]>a[i+1])
这里出问题了
当j=0,i=4时,a[i+1]是哪个元素啊~这时不是越界了。
所以我感觉应该是这样写:
template <typename T>
void sort(T v[],int size)
{
for(int i = 0;i < size;++i)
for(int j = i;j < size - 1-i;++j)
if(a[j]>a[j+1])
swap(a[j],a[j+1]);//标准库里好像有这个函数
}

用模板写就省很多功夫了

sandy 2007-04-27 11:03 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/22/22451.html#22577chengerchengerSun, 22 Apr 2007 02:13:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/22/22451.html#22577template <typename T>
void sort(T v[],int size)
{
for(int i = 0;i < size;++i)
{
for(int j = i;j < size - 1;++j)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);//标准库里好像有这个函数
}
}
}

不知道对不对……这个sort,如果代码没错的话,可以适用于所有能够用<比较大小的类型,此外要支持赋值,总之要让swap函数能工作。当然,更好的办法是提供两个迭代器begin,end指明排序范围。建议楼主去看看STL里算法的实现。

chenger 2007-04-22 10:13 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/22/22451.html#22574jarod-pkujarod-pkuSun, 22 Apr 2007 01:11:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/22/22451.html#22574
另外,VC6的库比较老,对"iostream"的支持不好。一般都是用.h的老库。

写成template要方便很多。


jarod-pku 2007-04-22 09:11 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22511踏雪赤兔踏雪赤兔Sat, 21 Apr 2007 10:57:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22511

踏雪赤兔 2007-04-21 18:57 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22495pengkunypengkunySat, 21 Apr 2007 07:36:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22495{
for(i=0;i<5-j;i++)
{
if(a[i]>a[i+1])
...}
}
怎么没越界,j = 0, i = 4, a[i+1]越界a[5]!
我前面写错了, 外循环不要改, 改后如下:
for(j=0;j<5;j++)
{
for(i=0;i<4-j;i++) //内循环改一下
{
if(a[i]>a[i+1])
...}
}


我运行结果良好,怎么会调不出来

pengkuny 2007-04-21 15:36 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22493tivili_chentivili_chenSat, 21 Apr 2007 06:49:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22493#include<string>
using namespace std;
void sort(long []);
void sort(int []);
void sort(float []);
int main()
{ long a[5]={10100, -123567, 1198783, -165654, 3456};
int b[5]={1,9,0,23,-45};
float c[5]={2.4f,7.6f,5.5f,6.6f,-2.3f};

sort(a);
sort(b);
sort(c);
return 0;
}

void sort(long a[])
{int i,j;
long t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if(a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}

void sort(int a[])
{int i,j,t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if(a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}

void sort(float a[])
{int i,j;
float t;
for(j=0;j<5;j++)
for(i=0;i<5-j;i++)
if(a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
}



tivili_chen 2007-04-21 14:49 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22491linxuexlinxuexSat, 21 Apr 2007 06:38:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22491若j<4 j取0,1,2,3四个数
我调了一下你给的 没调出来



能不能帮我再调一下 谢谢!!!!!!!!!!!!!

linxuex 2007-04-21 14:38 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22458wzqxp2002wzqxp2002Sat, 21 Apr 2007 01:57:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22458

wzqxp2002 2007-04-21 09:57 发表评论
]]>
re: 请 教http://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22457pengkunypengkunySat, 21 Apr 2007 01:52:00 GMThttp://www.cppblog.com/linxuex/archive/2007/04/21/22451.html#22457#include "stdafx.h"
#include<iostream>
//#include<string>

using namespace std;

int main()
{
long a[5]={10100, -123567, 1198783, -165654, 3456};
int b[5]={1,9,0,23,-45};
double c[5]={2.4,7.6,5.5,6.6,-2.3};
void sort(long []);
void sort(int []);
void sort(double []);
sort(a);
sort(b);
sort(c);
return 0;
system("pause");
}

void sort(long a[])
{
int i,j;
long t;
for(j=0;j<4;j++)
{
for(i=0;i<4-j;i++)
{
if(a[i]>a[i+1])
{
t=a[i];a[i]=a[i+1];a[i+1]=t;
}
}
}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
system("pause");
}

void sort(int a[])
{
int i,j,t;
for(j=0;j<4;j++)
{
for(i=0;i<4-j;i++)
{
if(a[i]>a[i+1])
{
t=a[i];a[i]=a[i+1];a[i+1]=t;
}
}
}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
system("pause");
}

void sort(double a[])
{
int i,j;
double t;
for(j=0;j<4;j++)
{
for(i=0;i<4-j;i++)
{
if(a[i]>a[i+1])
{
t=a[i];a[i]=a[i+1];a[i+1]=t;
}
}
}
cout<<"the sorted numbers:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl<<endl;
system("pause");
}

pengkuny 2007-04-21 09:52 发表评论
]]>