【STL】 map、hash_map、迭代器
来源:互联网 发布:剪刀截图软件 编辑:程序博客网 时间:2024/06/06 20:42
1. hash_map和map的区别在哪里?
构造函数。hash_map需要hash函数,等于函数;map只需要比较函数(小于函数).
存储结构。hash_map采用hash表存储,map一般采用红黑树(RB Tree)实现。因此其内存数据结构是不一样的。
2. 什么时候需要用hash_map,什么时候需要用map?
选用map还是hash_map,关键是看关键字查询操作次数,以及你所需要保证的是查询总体时间还是单个查询的时间。
如果是要很多次操作,要求其整体效率,那么使用hash_map,平均处理时间短。
如果是少数次的操作,那么使用平均处理时间相对较慢、单次处理时间恒定的map,考虑整体稳定性应该要高于整体效率,因为前提在操作次数较少。
3.迭代器
<iterator> 中定义了迭代器得使用方法,主要得模版类有:
插入迭代器
插入迭代器是一种迭代器适配器,带有一个容器参数,并生成一个迭代器,用于在指定的容器中插入元素。通过插入迭代器赋值时,迭代器将会插入一个新的元素。C++语言提供了三种插入器,其差别在于插入元素的位置不同:
#include<iostream>
#include <vector>
#include <iterator>
using namespacestd;
int main(){
istream_iterator<int> in_iter(cin);
istream_iterator<int>eof;
//vector<int> vec(in_iter, eof); //do the same work as following loop
vector<int> vec;
while (in_iter!=eof)
vec.push_back(*in_iter++); //*in_iter可以取出输入流中得元素
vector<int>::const_iterator it = vec.begin();
for(; it!= vec.end();++it)
cout<<*it<<endl;
return 0;
}
***********
#include<iostream>
#include <iterator>
using namespacestd;
int main(){
ostream_iterator<string> out_iter(cout,""n");
istream_iterator<string> in_iter(cin),eof;
while (in_iter!=eof)
*out_iter++=*in_iter++;
return 0;
}
#include <string>
#include <iterator>
#include <algorithm>
using namespace std;
string str = "this 'sentence' is a test";
cout<<str<<endl;
string::iterator it1 = find(str.begin(), str.end(), '"'');
string::iterator it2 = find(++it1, str.end(), '"'');
cout<<string(it1, it2)<<endl;
string::reverse_iterator rit1 = find(str.rbegin(), str.rend(), '"'');
string::reverse_iterator rit2 = find(++rit1, str.rend(), '"'');
cout<<string(rit1, rit2)<<endl;
cout<<string(rit2.base(), rit1.base())<<endl;
getchar();
return 0;
}
(input iterator)读,不能写。支持的操作集:==, !=, 前缀++, 后缀++, *, ->。例如,find, accumulate算法要求这类迭代器。输出迭代器
(output iterator)写,不能读。支持的操作集:前缀++, 后缀++, *(只能出现在赋值运算的左操作数上)。推出迭代器要求每个迭代器必须正好写入一次。例如,ostream_iterator是输出迭代器,copy算法使用这类迭代器。前向迭代器(forward iterator)读和写,支持输入迭代器和输出迭代器提供的所有操作,还支持对同一个元素的多次读写。例如,replace算法需要这种迭代器。双向迭代器(bidirectional iterator)读和写,除了支持前向迭代器的所有操作,还支持前缀--和后缀--,即支持双向遍历容器。例如,reverse算法要求这类迭代器。标准库容器中提供的迭代器都至少达到双向迭代器的要求。随机访问迭代器(random-access iterator)读和写。提供在常量时间内访问容器任意位置的功能。支持完整的迭代器操作集:1)关系运算:==, !=, <, <=, >, >=;2)算术运算:it + n, it - n, it += n, it -= n以及it1 - it2;3)下标运算:it[n],等价于*(it + n)。需要随机访问迭代器的泛型算法包括sort算法。例如,vector, deque, string迭代器是随机访问迭代器,用作访问内置数组元素的指针也是随机访问迭代器。
- 【STL】 map、hash_map、迭代器
- STL:map与hash_map
- STL map 和 hash_map
- [STL基础]map映射,hash_map
- STL中的map、unordered_map、hash_map
- hash_map快于STL中的map
- map、hash_map、迭代器
- STL map 、PHP HashTable、hash_map 性能比较
- 关于STL中的map和hash_map
- 详解STL中的map和hash_map区别
- 关于STL中的map和hash_map
- 关于STL中的map和hash_map
- STL中map 和 hash_map 的使用方法
- C++ STL中哈希表Map 与 hash_map 介绍
- C++ STL中哈希表Map 与 hash_map 介绍
- STL hash_map
- STL---hash_map
- STL hash_map
- 与“老大”一起谈软件行业
- XHTML与HTML区别是什么
- 《Oracle编程艺术》学习笔记(8)-Oracle中的文件-警告日志文件
- [设计原则] 通过机制解决问题
- 《Oracle编程艺术》学习笔记(9)-Oracle中的文件-数据文件和存储体系(表空间/段/区段/块)
- 【STL】 map、hash_map、迭代器
- CI php发送邮件
- 嵌入式系统 Boot Loader 技术内幕
- 什么是软件设计
- Linux超级用户root口令忘记怎么办?
- Oracle SQL Trace 和 10046 事件跟踪
- 腾讯面试题:买200返100优惠券,实际上折扣是多少?
- Tomcate三种部署项目的方法(转)
- 《Oracle编程艺术》学习笔记(10)-SGA,PGA,UGA和memory_target