unordered_map

作者: 影醉阏轩窗 | 来源:发表于2018-08-09 16:39 被阅读0次

    百度都是关于unorder_mapmap的差别等,主要讲效率什么的,而我只想快速的使用unorder_map而已。

    简要说明unorder_map

    1. 效率高
    2. 内部使用harsh原理构造
    3. 多用在关联性比较高的数据结构中
    4. 多用在查找、对比等算法中......

    函数简介

    使用类似python的字典,也就是key和value对应关系。

    unordered_map<Key,T>
    

    遍历unorder_map中的key和value:

       unordered_map<key,T>::iterator it;
        (*it).first;        //the key value
        (*it).second   //the mapped value
        for(unordered_map<key,T>::iterator iter=mp.begin();iter!=mp.end();iter++)
              cout<<"key value is"<<iter->first<<" the mapped value is "<< iter->second;
    
        也可以这样:
        for(auto& v : mp)
            print v.first and v.second
    
    • empty

      Test whether container is empty (public member function)

      true if the container size is 0, false otherwise.

      判断内部是否为空,返回bool类型

    • size

      Return container size (public member function)

      返回容器大小,类型unsigned integral

    • max_size

      Return maximum size (public member function)

      返回最大的数据endReturn const_iterator to end (public member function)


    • begin

      Return iterator to beginning (public member function)

      返回类型为迭代器,和vector的begin类似。

    • end

      Return iterator to end (public member function)

    • cbegin

      Return const_iterator to beginning (public member function)

      于上面begin的却别在于cbegin=const begin 也就是不可改变的,其实用法一样的。

    • cend

      Return const_iterator to end (public member function)


    • find

      Get iterator to element (public member function)

      注意这里返回是迭代器,可以和emplace_hint等使用迭代器做参数的函数一起使用。

    • count

      Count elements with a specific key (public member function )

      1 if an element with a key equivalent to k is found, or zero otherwise.

      存在返回1,不存在返回0.

    • equal_range

      Get range of elements with specific key (public member function)

        若有unordered_map<int, int> mp;查找x是否在map中
        //方法1:  若存在  mp.find(x)!=mp.end()
        //方法2:  若存在  mp.count(x)!=0
    

    • emplace

      Construct and insert element (public member function )

      插入数据:mymap.emplace ("NCC-1701", "J.T. Kirk");

    • emplace_hint

      Construct and insert element with hint (public member function )

      这里和上面的find结合使用,出入的值包括一个迭代器。

      mymap.emplace_hint (iter,"NCC-1701", "J.T. Kirk");

    • insert

      Insert elements (public member function )

       myrecipe.insert (std::make_pair<std::string,double>("eggs",6.0)); // move insertion
       myrecipe.insert (mypantry.begin(), mypantry.end());  // range insertion
      
      
    • erase

      Erase elements (public member function )

      // erase examples:
        mymap.erase ( mymap.begin() );      // erasing by iterator
        mymap.erase ("France");             // erasing by key
        mymap.erase ( mymap.find("China"), mymap.end() ); // erasing by range
      
    • clear

      Clear content (public member function )

      All the elements in the unordered_map container are dropped: their destructors are called, and they are removed from the container, leaving it with a size of 0.

      全部清除!

    • swap

      Swap content (public member function)

      std::unordered_map<std::string,std::string>
           first = {{"Star Wars","G. Lucas"},{"Alien","R. Scott"},{"Terminator","J. Cameron"}},
           second  = {{"Inception","C. Nolan"},{"Donnie Darko","R. Kelly"}};
      
        first.swap(second);
      
        std::cout << "first: ";
        for (auto& x: first) std::cout << x.first << " (" << x.second << "), ";
        std::cout << std::endl;
      
        std::cout << "second: ";
        for (auto& x: second) std::cout << x.first << " (" << x.second << "), ";
        std::cout << std::endl;
      

      交换全部key和value,很少用到。


    • bucket_count

      Return number of buckets (public member function)

    • max_bucket_count

      Return maximum number of buckets (public member function)

    • bucket_size

      Return bucket size (public member type)

    • bucket

      Locate element's bucket (public member function)


    Hash policy

    • load_factor

      Return load factor (public member function)

    • max_load_factor

      Get or set maximum load factor (public member function )

    • rehash

      Set number of buckets (public member function )

    • reserve

      Request a capacity change (public member function)

    后面两个暂时还不懂什么意思,之后用到再进行总结。

    参考链接

    这几次写文章都忘记写参考链接,非常抱歉!!!

    一个简单介绍文章

    官网

    相关文章

      网友评论

        本文标题:unordered_map

        本文链接:https://www.haomeiwen.com/subject/fadcbftx.html