c++排序函数怎么使用

shuangyuhacker 2009-06-19 04:33:41
我现在有一个数字数组a,长度为10.
如何使用sort函数将其进行排序,并且将排序后的值重新赋值给这个数组a
...全文
238 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mstlq 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yang_e_2009 的回复:]
sort函数似乎不可以直接用在数组上吧
C/C++ code
template<class _RanIt,
class _Pr> inline
void sort(_RanIt _First, _RanIt _Last, _Pr _Pred);
[/Quote]

这个……
是可以的,因为指针满足随机迭代器的性质啊^_^
Jalien 2009-06-19
  • 打赏
  • 举报
回复
这个我觉得查看msdn很方便的。
mstlq 2009-06-19
  • 打赏
  • 举报
回复

#include <algorithm>
using namespace std;

int a[10];
//初始化a数组

sort(&(a[0]),&(a[10]));//你没看错,是a[10]!
//好了,收工了……
yang_e_2009 2009-06-19
  • 打赏
  • 举报
回复
sort函数似乎不可以直接用在数组上吧

template<class _RanIt,
class _Pr> inline
void sort(_RanIt _First, _RanIt _Last, _Pr _Pred);
lingyin55 2009-06-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int main()
{
vector<string> strArray ;
string str[4] = {"hello, world!", "welcome to cpp.", "effective c++", "exceptional c++"};

for(int i = 0; i<4; i++)
strArray.push_back(str[i]);


sort(strArray.begin(), strArray.end());

vector<string>::iterator st;
for(st = strArray.begin(); st != strArray.end(); st++)
cout<<*st<<endl;

system("pause");
return 0;
}
woods2001 2009-06-19
  • 打赏
  • 举报
回复
你用vector 在用sort
不然你自己写个冒泡排序 不也很简单吗
帅得不敢出门 2009-06-19
  • 打赏
  • 举报
回复

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int a[7] = {23, 1, 33, -20, 6, 6, 9};

sort(a, a+7);

for (int i=0; i<7; i++) {
cout << a[i] << " ";
}

return 0;
}



This prints the sorted values.
-20 1 6 6 9 23 33
shuangyuhacker 2009-06-19
  • 打赏
  • 举报
回复
关键我是初学者,您说的STL我不懂啊。
我只知道C++有个内部函数可以将数组排序
Sco_field 2009-06-19
  • 打赏
  • 举报
回复
既然c++为什么还用低级的数组,直接交给STL处理好了

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧