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 ...
随机推荐
- 转-CSS优先级(权重)解析
1.多个选择器可能会选择同一个元素,有3个规则,从上到下重要性降低: !important的用户样式 !important的作者样式 作者样式 用户样式 浏览器定义的样式 2. CSS规范为不同类型的 ...
- JS+CSS简单实现DIV遮罩层显示隐藏
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- java 读取excel 将数据插入到数据库
import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Con ...
- Introducing 'bind'
原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-bind/ 上一篇讨论了如何理解let作为一个能实现contin ...
- 实现类似QQ自拍头像的功能(demo源码)
在很多软件系统中,都允许用户设置自己的头像,甚至可以直接使用摄像头照相作为自己的头像,就像QQ的自拍头像功能一样. 这种功能是如何实现的了?最直接的,我们可以使用Windows提供的VFW技术或Dir ...
- Excel教程(5) - 日期与时间函数
DATE 用途:返回代表特定日期的序列号. 语法:DATE(year,month,day) 参数:year 为一到四位,根据使用的日期系统解释该参 数.默认情况下,Excel for Windows ...
- pc app 桌面打包
进入 http://nwjs.io/ 下载 创建web项目,在项目根目录 创建文件package.json并填写 1 2 3 4 5 6 7 { "name": " ...
- Just do it!!!
4月英语竞赛期中考试 5月初级程序员考试 6月英语四级考试 7月期末考试 加油吧年轻人!
- erlang程序优化点的总结(持续更新)
转自:http://wqtn22.iteye.com/blog/1820587 转载请注明出处 注意,这里只是给出一个总结,具体性能需要根据实际环境和需要来确定 霸爷指出,新的erlang虚拟机有很多 ...
- 2.按要求编写Java应用程序: (1)编写西游记人物类(XiYouJiRenWu) 其中属性有:身高(height),名字(name),武器(weapon) 方法有:显示名字(printName),显示武器(printWeapon) (2)在主类的main方法中创建二个对象:zhuBaJie,sunWuKong。并分别为他 们的两个属性(name,weapon)赋值,最后分别调用printNam
XiYouJiRenWu package com.hanqi.test; public class XiYouJiRenWu { String height,name,weapon; XiYouJiR ...