C++中vector,set,map自定义排序
一、vector排序
vector支持cmp,就类似数组,可以直接sort。
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <map>
#include <queue>
#include <stack>
#include <set>
typedef long long ll;
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main()
{
cout << "VECTOR" << endl;
vector<int> v;
v.push_back();
v.push_back();
v.push_back();
v.push_back();
sort(v.begin(), v.end(), cmp);
for(vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
}
二、set排序,不可以使用sort,可以直接定义的时候就设置优先级
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <map>
#include <queue>
#include <stack>
#include <set>
typedef long long ll;
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main()
{
set<int, greater<int> > s;
s.insert();
s.insert();
s.insert();
s.insert();
cout << "SET" << endl;
for(set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << endl;
} }
三、map自定义排序,也不能用sort,目前我只了解根据key排序,按照value还有待学习
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <math.h>
#include <map>
#include <queue>
#include <stack>
#include <set>
typedef long long ll;
using namespace std;
bool cmp(int a, int b) {
return a > b;
}
int main()
{
cout << "MAP" << endl;
map<char, int, greater<char> > m;
m['c'] = ;
m['b'] = ;
m['a'] = ;
for(map<char, int>::iterator it = m.begin(); it != m.end(); it++) {
cout << it->first << " " << it->second << endl;
} }
C++中vector,set,map自定义排序的更多相关文章
- CCF CSP 201503-2 数字排序 (map+自定义排序)
题目链接:http://118.190.20.162/view.page?gpid=T26 返回试题列表 问题描述 试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: ...
- std::map 自定义排序
PS:开发中难免会用到快速检索的数据结构-map , 很多时候map自身提供的排序不能满足我们的需要或者不支持我们自定的数据结构的排序,解决办法就是自己实现排序. 这里的小案例是:我们要经用户的has ...
- STL中vector,Map,Set的实现原理
vector的数据安排以及操作方式,与array非常类似,两者唯一的区别是空间运用的灵活性,array是静态空间,一旦配置了就不能改变,如果你想要大一点的空间,就必须首先配置一块新空间,然后将原来的元 ...
- C/C++知识要点2——STL中Vector、Map、Set容器的实现原理
1.Vector是顺序容器.是一个动态数组.支持随机存取.插入.删除.查找等操作,在内存中是一块连续的空间.在原有空间不够情况下自己主动分配空间.添加为原来的两倍.vector随机存取效率高,可是在v ...
- Qt之QHeaderView自定义排序(获取正确的QModelIndex)
简述 前几节中分享过关于自定义排序的功能,貌似我们之前的内容已经可以很好地解决排序问题了,但是,会由此引发一些很难发现的问题...比如:获取QModelIndex索引错误. 下面,我们先来实现一个整行 ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase
今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0 这个程序使用了vect ...
- Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法
Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...
- 关于C++中vector和set使用sort方法进行排序
C++中vector和set都是非常方便的容器, sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序 将sort方法用到vector和set中能实现多种 ...
随机推荐
- sass文件处理
sass注释方式有两种: 1.标准的css注释/**/: 2.//双斜杠形式的单行注释(不会被转译): 标准的css注释 双斜杆单行注释 sass文件后缀名有两种: 1.后缀名为sass,不适用用大括 ...
- iDempiere 视频教程下载
Created by 蓝色布鲁斯,QQ32876341,blog http://www.cnblogs.com/zzyan/ iDempiere官方中文wiki主页 http://wiki.idemp ...
- Android Studio笔记之快捷键
Android Studio h2{ color: #4abcde; } pre{ background-color: #f8f8f8; border: solid 1px #ccc; border- ...
- android的MVP模式
MVP简介 相信大家对MVC都是比较熟悉了:M-Model-模型.V-View-视图.C-Controller-控制器,MVP作为MVC的演化版本,那么类似的MVP所对应的意义:M-Model-模型. ...
- pcharm激活
服务器搭建命令:(一直默认即可) wget http://home.ustc.edu.cn/~mmmwhy/jetbrain.sh && sh ./jetbrain.sh 我搭建的服务 ...
- 深入Python(1): 字典排序 关于sort()、reversed()、sorted()
http://www.cnblogs.com/BeginMan/p/3193081.html 一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠 ...
- springMVC通过ajax传参到后台
JSON对象和JSON字符串 在SpringMVC环境中,@RequestBody接收的是一个Json对象的字符串,而不是一个Json对象.然而在ajax请求往往传的都是Json对象,用 JSON.s ...
- SVN:验证位置时发生错误解决方案
1. 2. 3.preferencens > svn >svn接口-选择SVNKit(Pure Java)设置后,再引用svn路径后,直接弹出输入用户名和密码就对了. 4.
- 使用slmgr查看、删除windows 授权(key)
查看 slmgr.vbs /dlv 删除授权 使用管理员权限进入cmd All program -> accessories -> Command Prompt (右键 已管理员方式运行) ...
- June 16th 2017 Week 24th Friday
Progress is the activity of today and the assurance of tomorrow. 进步是今天的活动,明天的保证. The best preparatio ...