散列表是一类经常使用的查找技术,在编程中也经常使用,这里简单谈谈基于map的散列表的实现。映射(map)是STL的一个关联容器,它提供一对一(第一个为关键字,每个关键字只能出现一次;第二个为该关键字的值,即key=value的形式)的数据处理能力
1、map的构造函数
map常用的构造函数如下:
1 map<int, char*> mapStudent; //int表示关键字 char*表示键值 mapStudent表示容器对象
2、数据的插入
map的常用插入数据方法有三种
- 用insert函数插入pair数据
- 用insert函数插入value_type数据
- 用数组方式插入数据
实现代码如下:
#include<iostream>
#include<cstdlib>
#include<string>
#include<map>
using namespace std;
int main()
{
map<int, char*> mapStudent; //int表示关键字 char*表示键值 mapStudent表示容器对象
mapStudent.insert(pair<int,char*>(1,"student_one")); //利用pair插入数据
mapStudent.insert(map<int,char*>::value_type(2, "student_two"));
mapStudent[3] = "student_three";
//mapStudent.insert(pair<int, char*>(3, "student_three"));
map<int, char*>::iterator iter;
for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
cout << iter->second << endl;
system("pause");
return 0;
}
3、map的大小
可以使用size函数
int nSize=mapStudent.size();
4、数据的遍历
有两种方法:1、采用迭代器,上面已经说过;2、用数组的方式,实现代码如下:
int nSzie=mapStudent.size();
for(int nIndex=0;nIndex<nSize;nIndex++)
{
cout<<mapStudent[nIndex];
}
原文链接: https://www.cnblogs.com/dapeng-bupt/p/7874093.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/263826
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!