一、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自定义排序的更多相关文章

  1. CCF CSP 201503-2 数字排序 (map+自定义排序)

    题目链接:http://118.190.20.162/view.page?gpid=T26 返回试题列表 问题描述 试题编号: 201503-2 试题名称: 数字排序 时间限制: 1.0s 内存限制: ...

  2. std::map 自定义排序

    PS:开发中难免会用到快速检索的数据结构-map , 很多时候map自身提供的排序不能满足我们的需要或者不支持我们自定的数据结构的排序,解决办法就是自己实现排序. 这里的小案例是:我们要经用户的has ...

  3. STL中vector,Map,Set的实现原理

    vector的数据安排以及操作方式,与array非常类似,两者唯一的区别是空间运用的灵活性,array是静态空间,一旦配置了就不能改变,如果你想要大一点的空间,就必须首先配置一块新空间,然后将原来的元 ...

  4. C/C++知识要点2——STL中Vector、Map、Set容器的实现原理

    1.Vector是顺序容器.是一个动态数组.支持随机存取.插入.删除.查找等操作,在内存中是一块连续的空间.在原有空间不够情况下自己主动分配空间.添加为原来的两倍.vector随机存取效率高,可是在v ...

  5. Qt之QHeaderView自定义排序(获取正确的QModelIndex)

    简述 前几节中分享过关于自定义排序的功能,貌似我们之前的内容已经可以很好地解决排序问题了,但是,会由此引发一些很难发现的问题...比如:获取QModelIndex索引错误. 下面,我们先来实现一个整行 ...

  6. 【转】c++中Vector等STL容器的自定义排序

    如果要自己定义STL容器的元素类最好满足STL容器对元素的要求    必须要求:     1.Copy构造函数     2.赋值=操作符     3.能够销毁对象的析构函数    另外:     1. ...

  7. STL中vector的赋值,遍历,查找,删除,自定义排序——sort,push_back,find,erase

    今天学习网络编程,那个程序中利用了STL中的sort,push_back,erase,自己没有接触过,今天学习一下,写了一个简单的学习程序.编译环境是VC6.0         这个程序使用了vect ...

  8. Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法

    Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...

  9. 关于C++中vector和set使用sort方法进行排序

    C++中vector和set都是非常方便的容器, sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序 将sort方法用到vector和set中能实现多种 ...

随机推荐

  1. HDU 4268 multiset

    http://acm.hust.edu.cn/vjudge/contest/123100#problem/B #include <iostream> #include <string ...

  2. ef linq 查询某时间段内数据 踩的坑

    var now = DateTime.Now;var list =db.Jinbi_TypeLimit.Where(x => x.IsAvailable && x.JinbiTy ...

  3. 重构指南 - 使用多态代替条件判断(Replace conditional with Polymorphism)

    多态(polymorphism)是面向对象的重要特性,简单可理解为:一个接口,多种实现. 当你的代码中存在通过不同的类型执行不同的操作,包含大量if else或者switch语句时,就可以考虑进行重构 ...

  4. 在asp.net中如何使用Session

    2.那么在asp.net中到底该怎么使用Session呢? Session对象用于存储从一个用户开始访问某个特定的aspx的页面起,到用户离开为止,特定的用户会话所需要的信息.用户在应用程序的页面切换 ...

  5. Picklist的配置

    Picklist包括静态的Picklist和动态的Picklist.静态Picklist的配置 1.首先在All Projects中,选中Picklist,复制一个Pistlist进行修改.Name( ...

  6. 如何开放 Azure 虚拟机 Ping 功能

    前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...

  7. Hadoop fs命令(转)

    最近使用hive做一些etl工作,除了日常sql的编写,了解hadoop及hive的一些底层原理性质的东西包括调优非常有必要,一次hive调优就把原来的零散文件做了合并.首先记下hadoop常用的命令 ...

  8. sqlserver学习3---sql函数

    一.SQL DML 和 DDL 可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL). SQL (结构化查询语言)是用于执行查询的语法.但是 SQL 语言也包含用于更新 ...

  9. OFBiz部署与运行

    下载及部署命令:http://ofbiz.apache.org/download.html OFBiz的安装部署是基于ant的,官网的案例是执行load-demo任务和start任务.以下分析是以12 ...

  10. php之Apache压力测试

    1,测试本机是否已经安装好Apache ①进入自己的Apache目录下面的bin目录,然后执行ab -V.如果返回Apache版本则表示已经装好 2,执行压力测试命令,ab -n 1000(请求总数) ...