C++学习记录之STL函数
来源:互联网 发布:淘宝让买家改评价 编辑:程序博客网 时间:2024/06/06 08:15
1、排序函数
sort (): 对给定区间所有元素进行排序
stable_sort: 对给定区间所有元素进行稳定排序
partial_sort: 对给定区间所有元素部分排序
partial_sort_copy: 对给定区间复制并排序 nth_element 找出给定区间的某个位置对应的元素 is_sorted 判断一个区间是否已经排好序 partition,使得符合某个条件的元素放在前面
stable_partition: 相对稳定的使得符合某个条件的元素放在前面
使用排序函数要加入#include <algorithm>头文件
(1)对基本类型数据从小到大排序
sort(vec.begin(),vec.end(),less<int>());
(2)对基本类型数据从大到小排序
sort(vec.begin(),vec.end(),greater<int>());
(3)对自定义结构体从小到大排序或者从大到小排序,自定义结构体必须重载operator <函数来指定按照什么规则来排序
struct tGame//自定义排序结构体{ int iNum; char name[20]; tGame() { memset(this,0,sizeof(tGame)); } bool operator <(const tGame &t) const//重载operator<函数指定按照iNum从小到大排序 { return iNum<t.iNum; }}std::vector<tGame> vec;vec.clear(); tGame RedGames[5]; RedGames[0].iNum=10; memcpy(RedGames[0].name,"RedCf ",sizeof(RedGames[0].name)); RedGames[1].iNum=20; memcpy(RedGames[1].name,"NiZhan ",sizeof(RedGames[1].name)); RedGames[2].iNum=20; memcpy(RedGames[2].name,"XuanWu ",sizeof(RedGames[2].name)); RedGames[3].iNum=50; memcpy(RedGames[3].name,"YingXiong ",sizeof(RedGames[3].name)); RedGames[4].iNum=200; memcpy(RedGames[4].name,"KeKeKeLe ",sizeof(RedGames[4].name)); for(int i=0;i<5;i++) { vec.push_back(RedGames[i]); } sort(vec.begin(),vec.end());
(4)对类排序
class Game { public: int iNum; char name[20]; Game() { iNum=0; memset(name,0,sizeof(name)); } bool operator <(const Game &t) const { return iNum>t.iNum; } }; std::vector<Game> vec; vec.clear(); Game RedGames[5]; RedGames[0].iNum=10; memcpy(RedGames[0].name,"RedCf ",sizeof(RedGames[0].name)); RedGames[1].iNum=20; memcpy(RedGames[1].name,"NiZhan ",sizeof(RedGames[1].name)); RedGames[2].iNum=20; memcpy(RedGames[2].name,"XuanWu ",sizeof(RedGames[2].name)); RedGames[3].iNum=50; memcpy(RedGames[3].name,"YingXiong ",sizeof(RedGames[3].name)); RedGames[4].iNum=200; memcpy(RedGames[4].name,"KeKeKeLe ",sizeof(RedGames[4].name)); for(int i=0;i<5;i++) { vec.push_back(RedGames[i]); } sort(vec.begin(),vec.end());
0 0
- C++学习记录之STL函数
- c++STL之排序函数
- c函数之【记录函数】
- STL学习之transform函数
- STL学习之函数对象
- C++STL之map学习
- C++STL之map学习
- C语言学习记录之八printf函数使用技巧
- STL学习之路之仿函数
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- C++STL之函数对象及谓词
- C++ STL之仿函数
- c++STL中常用模板常用函数记录
- STL 学习笔记 之 函数对象
- STL算法之accumulate函数学习
- 初学者学习C++STL之vector容器
- 数值优化(Numerical Optimization)学习系列-计算导数(Calculating Derivatives)
- myeclipse 导入项目 输入此项目名不能运行问题
- 小尾巴翻译APP产品分析
- HDU 5464 Clarke and problem(dp 动态规划)——BestCoder Round #56(div.1 div.2)
- Android工具栏--ActionBar
- C++学习记录之STL函数
- android为ImageView使用蒙层
- hibernate环境的搭建
- 第7章 虚拟机类加载机制
- 写一个输入框并将输入的结果在文本框中排序
- sqlserver的树形查询
- Linux 磁阵性能瓶颈定位过程
- Mybatis之配置文件中的objectFactory节点内容说明
- 欢迎使用CSDN-markdown编辑器