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 ...
随机推荐
- Jira 6.0.3安装破解汉化
前段时间和上海的朋友交流了下,他们公司使用JIRA管理项目.回来整理了下感觉很不错. http://www.unlimax.com/jira.html工作中总是有各种事务要去处理,而这些事务不仅仅是代 ...
- 为啥使用Iscroll.js之后,a不能触发点击事件?
原因:是iscroll.js阻止了a的行为. 解决方法:打开iscroll-probe.js,然后找到preventDefaultException方法. 然后修改为: // preventDefau ...
- c# listview的使用
C#向listview中添加项
- 浙大pat 1029题解
1029. Median (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incre ...
- kettle连接Hive中数据导入导出(6)
1.hive往外写数据 http://wiki.pentaho.com/display/BAD/Extracting+Data+from+Hive+to+Load+an+RDBMS 连接hive
- 理解 Storm 拓扑的并行度(parallelism)概念
组成:一个运行中的拓扑是由什么构成的:工作进程(worker processes),执行器(executors)和任务(tasks)! 在一个 Storm 集群中,Storm 主要通过以下三个部件来运 ...
- apicloud 初学
html5:在创建html时为了防止页面缩放等不兼容效果,要创建个viewport <meta name="viewport" content="maximum-s ...
- weblogic一些基本概念
<收藏过来的----------http://www.cnblogs.com/cocowool/archive/2012/04/01/2428861.html> WebLogic中的一些基 ...
- 【Machine Learning in Action --4】朴素贝叶斯分类
1.概述 朴素贝叶斯分类是贝叶斯分类器的一种,贝叶斯分类算法是统计学的一种分类方法,利用概率统计知识进行分类,其分类原理就是利用贝叶斯公式根据某对象的先验 概率计算出其后验概率(即该对象属于某一类的概 ...
- JPA 系列教程10-双向一对一关联表
双向一对一关联表的ddl语句 CREATE TABLE `t_person` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255 ...