• map,hash_map, hash_table, 红黑树 的原理和使用


        在刷算法题的时候总是碰到好多题,号称可以用hash table来解题。然后就蒙圈了。

    1.首先,map和hash_map的区别和使用

    (1)map底层用红黑树实现,hash_map底层用hash_table 实现。所以map的时间复杂度为O(logn), hash_map为O(1)。

    (2)map和hash_map都在stl中,直接include,但是在Mac系统中要#include <ext/hash_map>和 using namespace __gnu_cxx;

    (3)以map为例来说明使用(hash_map的使用方法和map一样的):

        a. 定义: map<int, string> person;

        b. 插入:person[1000] = "Rambo";  //直接插入,开销很大,先查找key为1000的value再插入Rambo

            person.insert(map<int, string>::value_type(1000, "Rambo"));  //开销较小

        c. 查找:使用find()和count()函数

            find()传入参数key,返回iterator,如果找不到,返回为end()。iterator数据类型为std::pair对象,iterator->first表示key,

            iterator->second表示value。

            

    int key = 123;
    map<int, string>::iterator it = person.find(key);
    if(it == person.end())
    //没找到
    else
    //找到了

        d.删除元素: person.erase(iterator it), person.erase(const Key& key); person.clear();

    2.hash table:

    3.红黑树:

    这两部分内容比较复杂,有空再弄吧。

  • 相关阅读:
    multiprocessing总结
    CPython在CPU密集型应用下的并发
    多线程交互
    线程等待与守护线程
    Python多线程(1)
    一个简单的单线程异步服务器
    多线程与多进程的实现
    socket的功能分割到不同函数
    数据处理项目Postmortem
    M2 终审
  • 原文地址:https://www.cnblogs.com/bubbler/p/5576783.html
Copyright © 2020-2023  润新知