stl排序函数用户自定义比较函数扩展
来源:互联网 发布:北平无战事曾可达 知乎 编辑:程序博客网 时间:2024/06/06 13:20
c++的STL的qsort函数,在自定义比较函数时,cmp函数如下:
int pstrcmp(const void *p1, const void *p2) { return strcmp((char*)(*(int*)(p1)), (char*)(*(int*)(p2))); }
测试函数如下:
#include<iostream>#include<cstring>#include<algorithm>using namespace std;int main(){ char **str=new char*[3]; str[0]=new char[8]; strcpy(str[0],"4873279"); str[1]=new char[8]; strcpy(str[1],"3101010"); str[2]=new char[8]; strcpy(str[2],"4873279"); printf("%x\n",str[1]); // printf("%d\n",str+1); // printf("%x\n",(char*)(*(int*)(str+1)));// output adress ,same with above // printf("%x\n",(*(char*)(str)));//output 40H // printf("%x\n",(char*)(*(char*)(str)));//output 40H qsort(str,3,8,pstrcmp); cout<<str[0]<<endl; cout<<str[1]<<endl; cout<<str[2]<<endl;return 0;}
去掉注释,看看输出结果.
这里只对
printf("%x\n",(char*)(*(char*)(str)));
printf("%x\n",(char*)(*(int*)(str+1)))
两个语句的不同结果,提出自己的困惑,还请大神指教!!
0 0
- stl排序函数用户自定义比较函数扩展
- STL 自定义比较函数
- C++ STL set自定义比较函数
- stl中map自定义比较函数
- C++ STL set自定义比较函数
- stl中map自定义比较函数
- usort( )函数—使用用户自定义的比较函数对数组中的值进行排序
- laravel创建或扩展用户自定义函数
- STL LIST中自定义排序函数例子
- PHP 使用用户自定义的比较函数对数组元素排序
- 01串排序。自定义比较仿函数
- C#排序函数和自定义比较器
- 128 Largest Number (自定义比较函数排序)
- stl中各种容器的自定义比较函数
- STL中sort、priority_queue、map、set的自定义比较函数
- STL中sort、priority_queue、map、set的自定义比较函数
- C++ 使用STL时自定义比较函数cmp
- STL set,map , priority_queue 及 lower_bound 的排序比较函数
- Shader学习之旅:入门介绍
- python入门小结-01
- Unicode与UTF-8/UTF-16
- 使用并行化:向量化
- 汇编跳转指令
- stl排序函数用户自定义比较函数扩展
- ValidateBox 验证框(表单验证)
- Android中的Intent
- .APPEND 和.INCLUDE有什么区别
- android 项目整体有错误,但是找不到具体错误的原因之一
- 第六周作业1——利用哈夫曼编码英文字母表
- ACdream群赛1112(Alice and Bob)
- NYOJ 873【环之最大和】
- Android-Universal-Image-Loader Task was Interrupted