map、hash_map、迭代器
来源:互联网 发布:网络销售平台策划方案 编辑:程序博客网 时间:2024/05/20 01:08
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、迭代器
- map、hash_map、迭代器
- map & hash_map
- STL:map与hash_map
- std::map & std::hash_map
- hash_map map 比较
- hash_map与map
- Map与Hash_map
- Map与Hash_Map
- STL map 和 hash_map
- map与hash_map
- hash_map和map
- hash_map 与map 区别
- hash_map和map
- hash_map 与map介绍
- map hash_map介绍
- map与hash_map
- map, hash_map,unordered_map介绍
- Sysdeo Eclipse Tomcat Launcher plugin 中DevLoader的用处(转载)
- 静静的在心里承受所有的伤痛和不快乐
- Android 应用开发笔记 - 单项选择(RadioGroup, RadioButton)
- linux的IO端口和IO内存
- iOS 中设置程序应用横竖屏设置
- map、hash_map、迭代器
- Android开发应用之照相机的使用
- C#中怎么判断一个数组中是否存在某个数组值
- ORACLE SQL——一句SQL搞定小计与总计
- c#开发中程序集调用时容易忽略的问题
- get_xpath
- 雁过也,月满西楼
- iphone获得系统邮箱地址
- C++技巧