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 ...
随机推荐
- Chapter 2 Open Book——25
"My name is Edward Cullen," he continued. "I didn't have a chance to introduce myself ...
- Java Lambda表达式入门[转]
原文链接: Start Using Java Lambda Expressions http://blog.csdn.net/renfufei/article/details/24600507 下载示 ...
- 浙大 pat 1007题解
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- React - Stores
Event emmiters that make data available, handle business logic, send events to React, and listen for ...
- C程序浅议
文件FILE是程序设计中的一个重要概念.所谓“文件”一般是指存储在外部介质上的数据的集合.操作系统是以文件为单位对数据进行管理的,而文件是以文件名为标识的.操作系统对文件实行“按名存取”. C语言把文 ...
- LeetCode OJ 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- CodeForces 697B Barnicle 模拟
强行模拟 纪念一下…… #include<stdio.h> #include<iostream> #include<algorithm> #include<m ...
- 解决XCode插件在XCode6.4上失效的办法
Xcode 6.4 解决 插件失效的方法 查看 插件目录: ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/ 邮件打开插件 ...
- HttpClient模拟get,post请求并发送请求参数(json等)
import java.io.IOException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org. ...
- Spring测试
测试类添加两个注解 @RunWith(SpringJUnit4ClassRunner.class)和@ContextConfiguration(locations = "classpath: ...