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 ...
随机推荐
- ASP.NET对HTML元素进行权限控制(三)
上一篇博客中有些没有考虑到的东西这次更改一下代码如下: 界面前台: <%@ Page Language="C#" AutoEventWireup="true&quo ...
- Oracle计算两个整数的和与这两个整数的差与商
PL/SQL(Procedural Language/SQL)是一种过程化语言. PL/SQL都是以(BLOCK)块为基本单位,整个PL/SQL块分为三部分 1.声明(Declare) 2.执行(以B ...
- 获得当前时间的PRO
1.没有参数的存储过程 create or replace procedure get_timeas cur_time varchar2(10);begin select to_char(sy ...
- 自定义Adapter
--MainActivity代码 package com.example.qqdemo; import java.util.ArrayList; import java.util.List; impo ...
- 修改mysql的root密码
use msyql; update user set password=password('新密码') where user='root'; flush privileges; quit net st ...
- Spring中后台字符串国际化
1.在工程的资源文件夹(source folder)中建立三个properties文件:messages.properties(默认).messages_zh_CN.properties(中文).me ...
- DBA应该知道的一些SQL Server跟踪标记
跟踪标记是什么? 对于DBA来说,掌握Trace Flag是一个成为SQL Server高手的必要条件之一,在大多数情况下,Trace Flag只是一个剑走偏锋的奇招,不必要,但在很多情况下,会使用这 ...
- google calendar
1. user guide on google https://developers.google.com/google-apps/calendar/instantiate 2. google app ...
- Scrum10-22
Time:2013-10-22 Author:居玉皓 Things we have done since yesterday's meeting: 在今天的Scrum中,STORY1 开发前期准备工作 ...
- UVA 10720 Graph Construction 贪心+优先队列
题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...