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 ...
随机推荐
- Distributing Parts
Distributing Parts 题目链接:http://codeforces.com/problemset/problem/496/E 贪心 将音乐和人都以低音升序排序,贪心处理低音更低的音乐, ...
- MVC中的Startup.Auth.cs、BundleConfig.cs、FilterConfig.cs和RouteConfig.cs
一.MVC中的Startup.Auth.cs.BundleConfig.cs.FilterConfig.cs和RouteConfig.cs四个文件在app_start中 <1>Bundle ...
- iOS开发讯飞语音的集成
1.进入官网注册账号,登陆,注册,应用. 2,下载sdk 导入系统库. 3,关闭bitcode 4,初始化讯飞语音. NSString * initString = [[NSString alloc ...
- Java中泛型 类型擦除
转自:Java中泛型是类型擦除的 Java 泛型(Generic)的引入加强了参数类型的安全性,减少了类型的转换,但有一点需要注意:Java 的泛型在编译器有效,在运行期被删除,也就是说所有泛型参数类 ...
- [SOJ] 1282. Computer games (KMP)
坑爹题 1282. Computer Game Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Brian is an ...
- BOOTICE(引导扇区维护工具) V1.3.3 中文免费绿色版
软件名称: BOOTICE(引导扇区维护工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 357KB图片预览: 软件简 ...
- Cash Machine
Problem Description A Bank plans to install a machine for cash withdrawal. The machine is able to de ...
- C Runtime Library来历, API, MFC, ATL关系
首先说明,我google了半天,想找到英文的关于这个资料,但是实在找不到,只好转载国人的讨论. CRT原先是指Microsoft开发的C Runtime Library,用于操作系统的开发及运行.后来 ...
- JavaScript JSON timer(计时器) AJAX HTTP请求 同源策略 跨域请求
JSON 介绍 1. JSON: JavaScript Object Notation 是一种轻量级的数据交换格式. 它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是 ...
- Android中调用系统的相机和图库获取图片
//--------我的主布局文件------很简单---------------------------------<LinearLayout xmlns:android="http ...