c++ map unordered_map
map
operator<的重载一定要定义成const。因为map内部实现时调用operator<的函数好像是const。
#include<string> #include<iostream> #include <string.h> #include <stdio.h> #include <stdlib.h> #include<map> using namespace std; struct person { string name; int age; person(string name, int age) { this->name = name; this->age = age; } bool operator < (const person& p) const { return this->age < p.age; } }; map<person,int> m; int main() { person p1(); person p2(); person p3(); person p4(); person p5(); m.insert(pair<person, )); m.insert(pair<person, )); m.insert(make_pair(p5, )); m.insert(make_pair(p1, )); m[p2] = ; for(map<person, int>::iterator iter = m.begin(); iter != m.end(); iter++) { cout<<iter->first.name<<"\t"<<iter->first.age<<endl; } getchar(); ; }
unordered_map
哈希map使用上和map区别不大,差别主要在性能上。
map采用红黑树的方式,而hash_map采用哈希的方法,
插入:: 所以map的插入和删除速率要比hash_map高,hash_map要做冲突处理。
查找:: 但是查找上hash_map就要比map的性能高很多,因为是哈希,所以可以直接按照内容找到,即查找的复杂度是O(1)。
#include<string> #include<iostream>#include<unordered_map> #include <algorithm> using namespace std; void myfunc( const pair< int, int >& obj) { std::cout << "first=" << obj.first << "second=" << obj.second <<std::endl; } unordered_map<int,int> m; int main() { m.insert(pair<, )); m.insert(pair<, )); m.insert(make_pair(, )); m.insert(make_pair(, )); m[] = ; for(auto iter = m.begin(); iter != m.end(); iter++) { cout<<iter->first<<"\t"<<iter->second<<endl; } for_each(m.begin(), m.end(), myfunc); getchar(); ; }
删除所有元素使用erase:
#include <string>
#include <map>
using namespace std;
{
int i;
map<char,string> obj;
map<char,string>::value_type mem1('a',"billchen");
obj.insert(mem1);
map<char,string>::iterator iter = obj.begin();
while(iter != obj.end()) //修改
{
iter->second="wang";
iter++;
}
iter=obj.begin();
while(iter != obj.end()) //遍历
{
cout << iter->first << endl;
cout << iter->second << endl;
iter++;
}
return 0;
}
map<int,string>::value_type mem1(1,"billchen");
map<int,string>::value_type mem2(2,"chenbaihu");
obj.insert(mem1);
obj.insert(mem2);
cout << obj.size() <<endl;
c++ map unordered_map的更多相关文章
- STL——map/unordered_map基础用法
map /multimap map是STL里重要容器之一. 它的特性总结来讲就是:所有元素都会根据元素的键值key自动排序(也可根据自定义的仿函数进行自定义排序),其中的每个元素都是<key, ...
- hash_map,map,unordered_map效率
利用unordered_map代替hash_map 实验环境 操作系统 fedora9 编译器版本 gcc4.3 实验方式 各种map使用插入和查找,比较速度和相关性能 代码 参考代码 下面测试说明了 ...
- map和unordered_map的差别和使用
map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/billcyj/article/details/7 ...
- 【转】Map 与 Unordered_map
map和unordered_map的差别和使用 map和unordered_map的差别还不知道或者搞不清unordered_map和map是什么的,请见:http://blog.csdn.net/b ...
- C++ map与unordered_map
map与unordered_map对比 map unordered_map 红黑树(非严格二叉平衡搜索树)实现 哈希表实现 有序 无序 -- 查找时间复杂度为O(1),非常快 空间消耗较大 空间消耗较 ...
- 原 c++中map与unordered_map的区别
c++中map与unordered_map的区别 头文件 map: #include < map > unordered_map: #include < unordered_map ...
- STL之map与pair与unordered_map常用函数详解
STL之map与pair与unordered_map常用函数详解 一.map的概述 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称 ...
- 关于c++ STL map 和 unordered_map 的效率的对比测试
本文采用在随机读取和插入的情况下测试map和unordered_map的效率 笔者的电脑是台渣机,现给出配置信息 处理器 : Intel Pentium(R) CPU G850 @ 2.90GHz × ...
- [C++]-map和unordered_map
转自:https://blog.csdn.net/BillCYJ/article/details/78985895 头文件不同 map: #include < map > unordere ...
随机推荐
- 第七十七节,CSS3前缀和rem长度单位
CSS3前缀和rem长度单位 学习要点: 1.CSS3前缀 2.长度单位rem 本章主要探讨HTML5中CSS在发展中实行标准化的一些问题,重点探讨CSS3中新属性前缀问题和新的单位rem. 一 CS ...
- Exception fixing docBase for context
原因在给tomcat的war包,传输过程中出了问题,或者不是2进制传输,重新用2进制传送到linux里的tomcat webapps 目录中 就解决了.
- JSP中 JSTL
1,JSTL是JSP的标准标签库的简称,JSP标准标签库包括5类:分别是核心标签库,国际化标签库,SQL标签库,XML标签库,函数标签库: 2,常用的就是核心标签库和SQL标签库: 核心标签库:前置名 ...
- Leetcode018 4Sum
public class S018 { //借鉴S015,速度有些慢 public List<List<Integer>> fourSum(int[] nums, int ta ...
- hdu 1407 测试你是否和LTC水平一样高
Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解. Inpu ...
- jquery-ui-multiselect 的使用
@model Gd.NetSign.Controllers.DTO.WsaleFundManageDTO @{ ViewBag.Title = "ShowDUYUN"; //Lay ...
- Alyona and mex
Alyona and mex time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- 711B - Chris and Magic Square 模拟
题目大意:在num[i][j]==0处填一个数使每行,每列,对角线的和相同,若果有多种答案输出一种. 题目思路:模拟 #include<iostream> #include<algo ...
- 对SNS网站现状和未来的一些想法——以我对人人网的体验为例
现在对人人网越来越没有兴趣了,上面的照片.状态也越来越少了,反而是朋友圈里大家比较活跃. 我觉得在网上发内容的,至少是希望得到大家关注的,可是为什么人人越来越被大家嫌弃了呢? 人人上的消息越来越被淹没 ...
- 获取集合、数组后要判断为空的必要性以及根据构造器创建对象后不是null的证实
在开发过程中,凡是获取到一个集合对象,在利用或者说遍历这个集合对象之前,都要进行是否为null以及size()>0的判断,但是如果size()>0的话,不是就已经可以说明此集合对象不为nu ...