C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等
上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm>

|
sort |
对给定区间所有元素进行排序 |
|
stable_sort |
对给定区间所有元素进行稳定排序 |
|
partial_sort |
对给定区间所有元素部分排序 |
|
partial_sort_copy |
对给定区间复制并排序 |
|
nth_element |
找出给定区间的某个位置对应的元素 |
|
is_sorted |
判断一个区间是否已经排好序 |
|
partition |
使得符合某个条件的元素放在前面 |
|
stable_partition |
相对稳定的使得符合某个条件的元素放在前面 |
sort(begin,end),表示一个范围,例如:
#include "stdafx.h"
#include <algorithm>
#include "iostream"
using namespace std; int main(int argc, char* argv[])
{
int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
for(i=0;i<11;i++)
cout<<a[i]<<',';
sort(a,a+11);
cout<<'\n';
for(i=0;i<11;i++)
cout<<a[i]<<',';
return 0;
}

输出结果将是把数组a按升序排序,说到这里可能就有人会问怎么样用它降序排列呢?这就是下一个讨论的内容.
一种是自己编写一个比较函数来实现,接着调用第三个参数的sort:sort(begin,end,compare)就成了。
对于list容器,这个方法也适用,把compare作为sort的参数就可以了,即:sort(compare).
1)自己编写compare函数:
#include "stdafx.h"
#include <algorithm>
#include "iostream"
using namespace std;
bool compare(int a,int b)
{
return a>b; //降序排列,如果改为return a<b,则为升序
} int main(int argc, char* argv[])
{
int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
for(i=0;i<11;i++)
cout<<a[i]<<',';
sort(a,a+11,compare);
cout<<'\n';
for(i=0;i<11;i++)
cout<<a[i]<<',';
return 0;
}

2)更进一步,让这种操作更加能适应变化。也就是说,能给比较函数一个参数,用来指示是按升序还是按降序排,这回轮到函数对象出场了。
为了描述方便,我先定义一个枚举类型EnumComp用来表示升序和降序。很简单:
enum Enumcomp{ASC,DESC};
然后开始用一个类来描述这个函数对象。它会根据它的参数来决定是采用“<”还是“>”
class compare
{
private:
Enumcomp comp;
public:
compare(Enumcomp c):comp(c) {};
bool operator () (int num1,int num2)
{
switch(comp)
{
case ASC:
return num1<num2;
case DESC:
return num1>num2;
}
}
};
接下来使用 sort(begin,end,compare(ASC)实现升序,sort(begin,end,compare(DESC)实现降序。
完整代码为
#include "stdafx.h"
#include <algorithm>
#include "iostream"
using namespace std;
enum Enumcomp{ASC,DESC}; class compare
{
private:
Enumcomp comp;
public:
compare(Enumcomp c):comp(c) {};
bool operator () (int num1,int num2)
{
switch(comp)
{
case ASC:
return num1<num2;
case DESC:
return num1>num2;
}
}
}; int main(int argc, char* argv[])
{
int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
for(i=0;i<11;i++)
cout<<a[i]<<',';
sort(a,a+11,compare(ASC));
cout<<'\n';
for(i=0;i<11;i++)
cout<<a[i]<<',';
return 0;
}

3)其实对于这么简单的任务(类型支持“<”、“>”等比较运算符),完全没必要自己写一个类出来。标准库里已经有现成的了,就在functional里,include进来就行了。functional提供了一堆基于模板的比较函数对象。它们是(看名字就知道意思了):equal_to<Type>、not_equal_to<Type>、greater<Type>、greater_equal<Type>、less<Type>、less_equal<Type>。对于这个问题来说,greater和less就足够了,直接拿过来用:
• 升序:sort(begin,end,less<data-type>());
• 降序:sort(begin,end,greater<data-type>()).
#include "stdafx.h"
#include <algorithm>
#include "iostream"
#include "functional"
using namespace std; int main(int argc, char* argv[])
{
int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
for(i=0;i<11;i++)
cout<<a[i]<<',';
sort(a,a+11,greater<int>());
cout<<'\n';
for(i=0;i<11;i++)
cout<<a[i]<<',';
return 0;
}

4)既然有迭代器,如果是string 就可以使用反向迭代器来完成逆序排列,程序如下:
#include "stdafx.h"
#include <algorithm>
#include "iostream"
#include "string"
using namespace std; int main(int argc, char* argv[])
{
string str="avfgrtty";
int i;
cout<<str<<'\n';
for (string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit)
cout << *rit;;
cout<<endl;
return 0;
}

qsort():快速排序算法
原型:
_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*));
解释: qsort ( 数组名 ,元素个数,元素占用的空间(sizeof),比较函数)
比较函数是一个自己写的函数 遵循 int com(const void *a,const void *b) 的格式。
当a b关系为 > < = 时,分别返回正值 负值 零 (或者相反)。
使用a b 时要强制转换类型,从void * 转换回应有的类型后,进行操作。
数组下标从零开始,个数为N, 下标0-(n-1)。
#include "stdafx.h"
#include <algorithm>
#include "iostream"
#include "string"
using namespace std; int compare(const void *a,const void *b)
{
return *(int*)b-*(int*)a;
} int main(int argc, char* argv[])
{
int a[11]={2,4,5,6,1,2,334,67,8,9,0},i;
for(i=0;i<11;i++)
cout<<a[i]<<',';
qsort((void *)a,11,sizeof(int),compare);
cout<<'\n';
for(i=0;i<11;i++)
cout<<a[i]<<',';
return 0; }

相关:
1)why你必须给予元素个数?
因为阵列不知道它自己有多少个元素
2)why你必须给予大小?
因为 qsort 不知道它要排序的单位.
3)why你必须写那个丑陋的、用来比较俩数值的函式?
因为 qsort 需要一个指标指向某个函式,因为它不知道它所要排序的元素型别.
4)why qsort 所使用的比较函式接受的是 const void* 引数而不是 char* 引数?
因为 qsort 可以对非字串的数值排序.
以上实例是基于vc6.0的
C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等的更多相关文章
- C++排序函数sort/qsort使用
问题描述: C++排序函数sort/qsort的使用 问题解决: (1)sort函数使用 注: sort函数,参数1为数组首地址,参数2是数组 ...
- C++ 排序函数 sort(),qsort()的使用方法
想起来自己天天排序排序,冒泡啊,二分查找啊,结果在STL中就自带了排序函数sort,qsort,总算把自己解脱了~ 所以自己总结了一下,首先看sort函数见下表: 函数名 功能描写叙述 sort 对给 ...
- C++ 排序函数 sort(),qsort()的用法
转自:http://blog.csdn.net/zzzmmmkkk/article/details/4266888/ 所以自己总结了一下,首先看sort函数见下表: 函数名 功能描述 sort 对给定 ...
- linux makefile字符串操作函数 替换subst、模式替换patsubst、去首尾空格strip、查找字符串findstring、过滤filter、反过滤filter-out、排序函数sort、取单词word、取单词串wordlist、个数统计words
1.1 字符操作函数使用 在Makefile中可以使用函数来处理变量,从而让我们的命令或是规则更为的灵活和具有智能.make所支持的函数也不算很多,不过已经足够我们的操作了.函数调用后,函 ...
- map正序、逆序排序
一.按 key 排序 1.map顺序排序(小的在前,大的在后): map<float,string,less<float> > m_aSort;//已float从小到大排序 2 ...
- 【C语言】写一个函数,实现字符串内单词逆序
//写一个函数,实现字符串内单词逆序 //比如student a am i.逆序后i am a student. #include <stdio.h> #include <strin ...
- (C++)STL排序函数sort和qsort的用法与区别
主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int widt ...
- 排序(sort qsort)
qsort() 函数: sort() 函数表: 函数名 功能描述 sort 对给定区间所有元素进行排序 stable_sort 对给定区间所有元素进行稳定排序 partial_sort 对给定区间所 ...
- [python学习] 语言基础—排序函数(sort()、sorted()、argsort()函数)
python的内建排序函数有 sort.sorted两个. 1.基础的序列升序排序直接调用sorted()方法即可 ls = list([5, 2, 3, 1, 4]) new_ls = sorted ...
随机推荐
- 查看Linux系统版本信息
一.查看Linux内核版本命令(两种方法): 1.cat /proc/version [root@S-CentOS home]# cat /proc/versionLinux version 2.6. ...
- Check for Data Duplicates on a Grid
Here is a piece of code to prevent duplicate data on a specific field on a page grid. You can of cou ...
- Cannot change version of project facet Dynamic web的解决方法
用Eclipse创建Maven结构的web项目的时候选择了Artifact Id为maven-artchetype-webapp,由于这个catalog比较老,用的servlet还是2.3的,而一般现 ...
- Silverlight学习之初始化参数
首先需要在Silverlight的宿主页面添加上initParams,如 <param name="initParams" value="key1=jerry,ke ...
- Asp.net从文件夹中读取图片,随机背景图
第一步:配置文件web.config里添加 <system.web><connectionStrings> <!--name 是自定义的,connectionString ...
- php判断是否为json格式的方法
php判断是否为json格式的方法. 首先要记住json_encode返回的是字符串, 而json_decode返回的是对象 判断数据不是JSON格式: 复制代码代码如下: function is_n ...
- PHP通过字符串调用函数
1. call_user_func function a($b,$c){ echo $b; echo $c; } call_user_func('a', "111","2 ...
- 转:javascript 中select的取值
javascript获取select的值全解 获取显示的汉字 document.getElementById("bigclass").options[window.document ...
- “Guess the number” game
项目描述:https://class.coursera.org/interactivepython-004/human_grading/view/courses/972072/assessments/ ...
- DB2查询结果显示n行
在SQLserver中语法是这样的:select top n * from staff ,即可查询显示n行数据 但是在DB2中语法是这样的,感觉比较接近英语. select * from STAFF ...