Searching and Sorting
来源:互联网 发布:上海日料放题 知乎 编辑:程序博客网 时间:2024/05/17 00:53
序
bsearch 和 _lfind参数3不同, 传值uCnt.
没有去单步bsearch, 从使用者角度看, 和_lfind一样.
实验
// test1958.cpp : Defines the entry point for the console application.// Searching and Sorting - bsearch#include "stdafx.h"#include <windows.h>#include <search.h>#include <string.h>#include <stdio.h>#include <stdlib.h>int compare1(const void* pAry, const void* ppArgToSearch){ return(_stricmp(*(char**)ppArgToSearch, (char*)pAry));}void testcase1() { char* result = NULL; char* key = "123"; UINT uCnt = 5; char* szMsgAry[6] = { "msg1", "msg2", "msg3", "msg4", "msg5", NULL, }; // if not found uCnt++, but can't add to ary, the elment was added is wrong... // if "123" not found, change szMsgary[5] 0x00333231, this is a bug... // 这函数没啥大用..., 只有list元素内容是内建数据类型(且不是指针)时,才好使. result = (char*)_lsearch(key, szMsgAry, &uCnt, sizeof(szMsgAry[0]), compare1); if (result) { printf("%s found\n", result); } else { printf("hello not found!\n"); }}int compare2(const void* pSearch, const void* ppArg){ int iSearch = *(int*)pSearch; int iAry = *(int*)ppArg; if (iSearch > iAry) { return 1; } else if (iSearch == iAry) { return 0; } else { return -1; }}void testcase2() { int* pFind = NULL; int key = 1940/*2017*/; UINT uCnt = 5; int iAry[6] = { 1900, 1910, 1920, 1930, 1940, 1950, }; // bsearch 和 _lfind参数3不同, 传值uCnt // 没有去单步bsearch, 从使用者角度看, 和_lfind一样 pFind = (int*)bsearch(&key, iAry, uCnt, sizeof(iAry[0]), compare2); if (NULL != pFind) { printf("%d found\n", *pFind); } else { printf("%d not found!\n", key); // 没找到会到这里 }}void main(unsigned int argc, char** argv){ // testcase1(); testcase2(); system("pause");}
0 0
- Sorting and Searching
- Searching and Sorting
- Searching and Sorting
- Searching and Sorting
- Searching and Sorting
- Sorting and Searching (1)----bubbleSort
- CareerCup Chapter 9 Sorting and Searching
- uva450-sorting&searching
- uva10420--sorting&searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 【索引】Sorting/Searching
- 比较,排序,搜索字符串对象( Comparing, Sorting, and Searching String Objects )CFString
- Sorting&Searching 旋转数组二分法查找 @CareerCup
- log and searching
- Searching and Ranking
- 单向链表实现堆栈
- Entity Framework Code First关系映射约定
- [Leetcode] #79 Word Search
- IEEE 802简介 802.11 802.15
- CENTOS7下设置oracle 11g自启动
- Searching and Sorting
- 欢迎使用CSDN-markdown编辑器
- 研究算法的重要性
- 秒杀多线程第一篇 多线程笔试面试题汇总
- c++实现设计模式中的观察者模式,削除代码的if else switch
- POJ 1010 STAMPS 笔记
- numpy.linspace 生成等差数组
- TCP/IP协议族(TCP/UDP的区别)
- 帮助你彻底理解值Java中的传递和引用传递