sqort函数用法总结
qsort包含在<stdlib.h>头文件中,此函数根据你给的比较条件进行快速排序,通过指针移动实现排序。排序之后的结果仍然放在原数组中。使用qsort函数必须自己写一个比较函数。
函数原型:
void qsort ( void * base, size_t num, size_t size, int ( * comparator ) ( const void *, const void * ) );
用法以及参数说明:
Sorts the num elements of the array pointed by base, each element size bytes long, using the comparator function to determine the order.
The sorting algorithm used by this function compares pairs of values by calling the specified comparator function with two pointers to elements of the array.
The function does not return any value, but modifies the content of the array pointed by base reordering its elements to the newly sorted order.
base Pointer to the first element of the array to be sorted.(数组起始地址)
num Number of elements in the array pointed by base.(数组元素个数)
size Size in bytes of each element in the array.(每一个元素的大小)
comparator Function that compares two elements.(函数指针,指向比较函数)
1、The function must accept two parameters that are pointers to elements, type-casted as void*. These parameters should be cast back to some data type and be compared.
2、The return value of this function should represent whether elem1 is considered less than, equal to, or greater than elem2 by returning, respectively, a negative value, zero or a positive value.
Return Value none (无返回值)
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
using namespace std; bool cmp_int(int a, int b)
{
return a > b;
} bool cmp_string(string a, string b)
{
return a > b;
} bool cmp_char(char a, char b)
{
return a > b;
} int main()
{
int num;
cin >> num;
int s[];
for (int i = ; i <=num;i++)
{
cin >> s[i];
}
sort(s + , s + + num,cmp_int);
for (int i =; i <=num; i++)
{
cout<< s[i]<<" ";
}
cout << endl;
string s1[];
for (int i = ; i <= num; i++)
{
cin >> s1[i];
}
sort(s1 + , s1 + + num, cmp_string);
for (int i = ; i <= num; i++)
{
cout << s1[i] << " ";
}
cout << endl;
char s2[];
cin >> s2;
int len = strlen(s2);
sort(s2, s2 + len, cmp_char);
cout << s2 << endl;
return ;
}
sqort函数用法总结的更多相关文章
- Oracle 中 decode 函数用法
Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...
- memcpy函数用法
memcpy函数用法 .分类: VC++ VC++ mfc matlab 2011-12-01 19:17 14538人阅读 评论(0) 收藏 举报 null 原型:extern void *memc ...
- Python回调函数用法实例详解
本文实例讲述了Python回调函数用法.分享给大家供大家参考.具体分析如下: 一.百度百科上对回调函数的解释: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函 ...
- php中opendir函数用法实例
这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 本文实例分析了php中opendi ...
- assert()函数用法总结
assert()函数用法总结 assert宏的原型定义在<assert.h>中,其作用是如果它的条件返回错误,则终止程序执行,原型定义: #include <assert.h> ...
- C语言中qsort函数用法
C语言中qsort函数用法-示例分析 本文实例汇总介绍了C语言中qsort函数用法,包括针对各种数据类型参数的排序,非常具有实用价值非常具有实用价值. 分享给大家供大家参考.C语言中的qsort ...
- OVER(PARTITION BY)函数用法
OVER(PARTITION BY)函数介绍 开窗函数 Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是:对于每个组返 ...
- qsort函数用法【转】
qsort函数用法 qsort 功 能: 使用快速排序例程进行排序 用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(con ...
- Perl Sort函数用法总结和使用实例
一) sort函数用法 sort LISTsort BLOCK LISTsort SUBNAME LIST sort的用法有如上3种形式.它对LIST进行排序,并返回排序后的列表.假如忽略了SUBNA ...
随机推荐
- Linux 批量解压gz包
[root@yoon export]# vi gunzip.sh !/bin/bashpath=/export/backup ----备份文件目录路径 for i in `ls ${path}/*`d ...
- git@oschina.net源代码管理使用日记
git的优势: 1 可以创建分支: 2 版本控制是基于每一次提交的,而不需要考虑每次提交了多少个文件. 下载: 下载网址为:http://git-scm.com/download,根据您的操作系统选择 ...
- [原创汉化]linux前端神器 WebStorm8 汉化
只汉化了linux版本 因为linux的工具没win多 不过汉化应该都通用的,自行尝试下. 汉化的不是很完全.有时间放出完全版本来.汉化是个体力活 转载随易,汉化不易,且转且注明 截图: http:/ ...
- ELF
http://www.360doc.com/content/11/0826/13/7588214_143424472.shtml 链接,装载都是基于数据结构ELF.
- semantic
cgfx 里会有这个 float4X4 View : View; :后面这个 view 是一种 叫做user defined semantic provide the correct data to ...
- web系统之session劫持解决
session劫持是一种比较复杂的攻击方法.大部分互联网上的电脑多存在被攻击的危险.这是一种劫持tcp协议的方法,所以几乎所有的局域网,都存在被劫持 可能. 两台主机要想进行TCP通信,必须经过一个三 ...
- 【WCF--初入江湖】12 WCF与Ajax编程
12 WCF与Ajax编程 Ajax Ajax基本原理 AJAX技术的本质原理就是:使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信. 通过这个对象,JavaS ...
- Checkbox框全选操作,form表单提交与jquery ajax提交两种处理方式
//1.jquery ajax<script type="text/javascript"> $(function(){ var basePath = $(" ...
- C# 构造函数的使用方法
C#构造函数是一个特殊的类方法.在很多方面,包括访问修饰符.重载以及参数列表的语法等方面,构造函数与普通的方法是类似的.然而,在使用方面以及行为方面,构造函数也具有许多特殊的语法和语义规则. 下面列出 ...
- hdu 4111 Alice and Bob 博弈论
这里有2种方法: 方法一:求SG函数 sg[i][j]:i表示1的个数,j表示合并操作的步数. 这共有4种操作: 1.消除一个1: 2.减掉一个1: 3.合并2个1: 4.把1合并到另外不是1中. 代 ...