转载:https://blog.csdn.net/sendinn/article/details/96286849

最近在项目中用标准库中的关联性容器map,但知道map默认升序的,但在一个需求时又不想让它排序,保持元素原始位置。原先查了资料发现,标注库中有不排序的map,可以重写map的第三个比较函数,但实际使用中发现,用了自定义的比较函数,map的find函数没法用。

unordered_map:

包含头文件 #include<unordered_map>

关联性:std::unorederd_map 是一个关联容器,其中的元素根据键来引用,而不是根据索引来引用。

无序性:在内部,std::unordered_map中的元素不会根据其键值或映射值按任何特定顺序排序,而是根据其哈希值组织到桶中,以允许通过键值直接快速访问各个元素(常量的平均时间复杂度)。

唯一性:std::unorederd_map中的元素的键是唯一的。

void testUnordermap()
{
std::unordered_map<std::string, int> unm;
unm["d"] = ;
unm["c"] = ;
unm["b"] = ;
unm["a"] = ;
unm["a"] = ; std::unordered_map<std::string, int>::iterator iter1 = unm.begin();
for (; iter1 != unm.end(); iter1++)
{
std::cout << "unordered_map: " << iter1->first.c_str() << std::endl;
} unm["j"] = ;
}

运行结果:

map:

在网上查资料说,map容器有4个参数,其中影响自动排序的是第三个参数,只要保证为true即可。

上给出的多是直接返回true或是if (lhs == rhs) return false; return true;(加了相同的key则默认处理返回false的条件)

但是其实map如果想第三个参数返回true需要经过两次比较,如果第一次返回true那么会把左右参数对调再判断一次,这一次则要返回false,才能无序排列,所以要多加些条件。

#include <iostream>
#include <unordered_map>
#include <map> template<class T>
struct DisableCompare : public std::binary_function<T,T,bool>
{
bool operator()(T lhs,T rhs)const
{
static bool disblecompare = false;
if (lhs == rhs)
{
return false;
}
if (disblecompare)
{
disblecompare = false;
return false;
}
else
{
disblecompare = true;
return true;
}
}
}; void test()
{
std::map<std::string, int, DisableCompare<std::string>> m;
m["d"] = ;
m["c"] = ;
m["b"] = ;
m["a"] = ;
m["a"] = ; std::map<std::string, int>::iterator iter = m.begin();
for (; iter != m.end();iter++)
{
std::cout << "map: " << iter->first.c_str() << std::endl;
}
std::map<std::string, int>::iterator iter1 = m.find("d");
} int main()
{
test();
getchar();
return ;
}

如果只想不排序,不用find的话,可以采用这种方法,但实际需求,一般都会使用map的find函数,所以在此还需要想其他办法。

也许不是这种特殊需求,我们只知道用map,并利用它的默认排序,知识的积累真是一点一滴的,没有捷径。

C++ std::map 屏蔽排序的更多相关文章

  1. 对std::map进行排序

    1.对Key排序. std::map的第三个参数即为对key进行排序的比较函数.默认为less,表示升序.如果要降序,可以改为greater. 2.对Value排序 不支持,因为map不是一个序列的容 ...

  2. std::map 自定义排序

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

  3. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  4. C++中vector,set,map自定义排序

    一.vector排序 vector支持cmp,就类似数组,可以直接sort. #include <iostream> #include <algorithm> #include ...

  5. std::map使用结构体自定义键值

    使用STL中的map时候,有时候需要使用结构题自定义键值,比如想统计点的坐标出现的次数 struct Node{ int x,y; }; ...... map<Node,int>mp; m ...

  6. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  7. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  8. C++ std::map::erase用法及其陷阱

    1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...

  9. std::map

    1.例: map<int,string> m_mapTest; m_mapTest.insert(make_pair(1,"kong")); m_mapTest.ins ...

随机推荐

  1. 设计模式 结构型 - 适配器模式 Adapter

    Adapter(适配器模式) ---- 加个“适配器”以便于复用 将一个类的接口转换成客户希望的另一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 应用场景 如果 ...

  2. css 带换行的垂直居中

    span{ display:flex; justify-content:left; align-items:center; height:100%; width:100%; }

  3. mysql的docker版本,如何通过docker run定制服务器选项

    一般用的是My.cnf文件. 如果要图省事呢? 以下的命令可供参考. 特别是--character-set-server=utf8 --collation-server=utf8_general_ci ...

  4. wordpress去掉自定义菜单的外层div

    wordpress调用自定义菜单时自动会在外层加一个<div class="menu-nav-container">,如下图所示,nav是后台定义的菜单名称,如果想把这 ...

  5. WGS84与CGCS2000坐标系

    1.WGS84,WGS是世界大地测量系统World Geodetic System的缩写,84是说此坐标系是1984年建立的:   2.自上世纪60年代,美国军方相继推出WGS60.WGS66.WGS ...

  6. (10)树莓派 vim编辑器使用

    进去后 1点击 insert 插入数据 2 ctrl+alt+c  粘贴内容 或者 手动敲入代码 3 ctrl+v     保存 4 :wq  保存退出 5 回车

  7. Let Start

      A free timing software with very small memory occupation. This tool is a pure green convenient off ...

  8. Anaconda与Python安装版本对应关系 --- 转载

    转载自:https://blog.csdn.net/yuejisuo1948/article/details/81043823 首先解释一下上表. anaconda在每次发布新版本的时候都会给pyth ...

  9. luogu 2742 二维凸包

    链接 luogu 模板一 上下利用斜率求凸包然后合并. #include <bits/stdc++.h> using namespace std; const int N=10005; c ...

  10. linux命令之------Mv命令

    Mv命令 1)作用:用来为文件或目录改名/或将文件或目录一如其他位置 2)-i:若指定目录已有同名文件,则先询问是否覆盖旧文件: 3)-f:在mv操作要覆盖某已有的目标文件时,不给任何指示: 4)案例 ...