zoukankan      html  css  js  c++  java
  • 散列表——基于c++中map容器的实现

     散列表是一类经常使用的查找技术,在编程中也经常使用,这里简单谈谈基于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];
    }
  • 相关阅读:
    SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    HAService 刨坑
    RocketMQ服务器监控误区
    Send [1] times, still failed
    RECONSUME_LATER
    RocketMQ 自定义文件路径
    RocketMQ 运维指令
    Thrift 学习记录
    服务网格(Service Mesh)学习记录
    Linux 安装 Apache
  • 原文地址:https://www.cnblogs.com/dapeng-bupt/p/7874093.html
Copyright © 2011-2022 走看看