在stm32开发可以调用c标准库的排序和查找 qsort bsearch
在嵌入式开发中,可以使用c标准库自带的库函数,而不用自己去早轮子,qsort 和bsearch就是其中的两个比较好用的
二分法查找,前提是已经排序好的数据。下面的代码, 如果数据为排序,则要进行排序后,再查找。
/* bsearch example */
#include <stdio.h> /* printf */
#include <stdlib.h> /* qsort, bsearch, NULL */
int compareints (const void *a, const void *b)
{
return ( *(int *)a - * (int *)b );
}
int values[] = { 50, 20, 60, 40, 10, 30 };
int main ()
{
int *pItem;
int key = 45;
qsort (values, 6, sizeof (int), compareints);
pItem = (int *) bsearch (&key, values, 6, sizeof (int), compareints);
if (pItem != NULL)
printf ("%d is in the array.\n", *pItem);
else
printf ("%d is not in the array.\n", key);
return 0;
}
快速排序
#include <stdio.h>
#include <stdlib.h>
int values[] = { 88, 56, 100, 2, 25 };
int cmpfunc (const void *a, const void *b)
{
return ( *(int *)a - * (int *)b ); //升序
//return ( *(int *)a - * (int *)b ); //降序
}
int main()
{
int n;
printf("排序之前的列表:\n");
for ( n = 0 ; n < 5; n++ )
{
printf("%d ", values[n]);
}
qsort(values, 5, sizeof(int), cmpfunc);
printf("\n排序之后的列表:\n");
for ( n = 0 ; n < 5; n++ )
{
printf("%d ", values[n]);
}
return (0);
}
比较函数需要特别注意~~~~
Pointer to a function that compares two elements.
This function is called repeatedly by qsort to compare two elements. It shall follow the following prototype:
|
|
Taking two pointers as arguments (both converted to const void*). The function defines the order of the elements by returning (in a stable and transitive manner):
| return value | meaning |
|---|---|
<0 |
The element pointed to by p1 goes before the element pointed to by p2 |
0 |
The element pointed to by p1 is equivalent to the element pointed to by p2 |
>0 |
The element pointed to by p1 goes after the element pointed to by p2 |
For types that can be compared using regular relational operators, a general compar function may look like:
|
|
在stm32开发可以调用c标准库的排序和查找 qsort bsearch的更多相关文章
- stm32存储器映像和标准库中定义外设地址的方法
结合存储器映像理解stm32标准库中定义外设地址的方法. stm32f103zet6是32位的.它所能访问的地址空间范围为2^32=4GB,把4GB分为8个block,分别为block0-block- ...
- 排序方法之标准库中的快排 qsort ()函数
C标准库qsort()函数的用法(快排) 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base, int nelem, int width, i ...
- android studio 1.0 开发 ndk 调用 c++ so库
一个没用过java和安卓的人使用android studio开发带c++ so库的安卓程序用例(以ndk的hello-jni为例),对于不熟悉java和安卓的人来说这个很花时间,希望通过这篇文章帮助跟 ...
- Linux golang使用cgo调用C++标准库问题
我们知道cgo无法直接调用c++方法,但是可以通过c包装c++方法,以达到使用的目的. C++中,我们经常会用到STL.在cgo中,如果要调用STL,需要作如下操作: //cgo LDFLAGS: - ...
- C++标准库 vector排序
前天要做一个对C++ STL的vector容器做一个排序操作,之前一直把vector当做一个容量可自动变化的数组,是的,数组,所以打算按照对数组进行排序的方法:用快速排序或是冒泡排序等算法自己写一个排 ...
- c运行库、c标准库、windows API的区别和联系
C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的. API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...
- c运行时库,c标准库,Windows系统api的关系
原文地址:http://blog.csdn.net/seastars_sh/article/details/8233324 C运行库和C标准库的关系 C标准库,顾名思义既然是标准,就是由标准组织制定的 ...
- (转)c运行库、c标准库、windows API的区别和联系
C运行时库函数C运行时库函数是指C语言本身支持的一些基本函数,通常是汇编直接实现的. API函数API函数是操作系统为方便用户设计应用程序而提供的实现特定功能的函数,API函数也是C语言的函数实现的 ...
- C++命名空间、标准库(std,全局命名空间)
背景 别人遇到的问题: C++ 全局变量不明确与 using namespace std 冲突 我遇到的问题与他相似,函数调用冲突 using namespace std; class compare ...
随机推荐
- 20175316 盛茂淞 2018-2019-2 《Java程序设计》实验五 《网络安全与编程》 实验报告
20175316 盛茂淞 2018-2019-2 <Java程序设计>实验五 <网络安全与编程> 实验报告 一.实验报告封面 课程:Java程序设计 班级:1753班 姓名:盛 ...
- 【Linux内核】编译与配置内核(x86)
[Linux内核]编译与配置内核(x86) https://www.cnblogs.com/jamesharden/p/6414736.html
- Appium+unittest+python登录app
代码: # coding=utf-8 from appium import webdriver import time import unittest import os import HTMLTes ...
- Feign【首次请求失败】
当feign和ribbon整合hystrix之后,可能会出现首次调用失败的问题,出现原因分析如下: hystrix默认的超时时间是1秒,如果接口请求响应超过这个时间,将会执行fallback,spri ...
- 新浪sae对storage的文档进行读写操作
有的人喜欢将一些数据写在服务器的文件里面,并不喜欢存在mysql里,但新浪sae却不支持对本地文件进行操作. 不过sae拓展了一个storage的服务,可以将一些静态文件放在上面.本文不介绍文件的上传 ...
- GB18030 字符集
gb18030 编辑 国家标准GB18030-2005<信息技术 中文编码字符集>是我国继GB2312-1980和GB13000.1-1993之后最重要的汉字编码标准,是我国计算机系统必须 ...
- JVM GC 算法原理(转)
出处: https://mp.weixin.qq.com/s/IfUFuwn8dsvMIhTS3V01FA 对于JVM的垃圾收集(GC),这是一个作为Java开发者必须了解的内容,那么,我们需要去了解 ...
- SpringBoot exception异常处理机制源码解析
一.Spring Boot默认的异常处理机制 1:浏览器默认返回效果 2:原理解析 为了便于源码跟踪解析,在·Controller中手动设置异常. @RequestMapping(value=&quo ...
- (二)SpringBoot之springboot开发工具的使用以及springboot插件的功能
一.springboot开发工具的使用 1.1 在项目中添加springoot开发工具 1.2 功能 修改代码后点击保存自动重启 二.springboot插件的功能 2.1 maven配置 <p ...
- js中new到底做了什么?
1.创建一个新的obj; 2.让obj_proto_=Func.prototype; 3.Func.call(obj);