手记

unordered_map

百度都是关于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,很少用到。



作者:影醉阏轩窗
链接:https://www.jianshu.com/p/3ceed1d67688


0人推荐
随时随地看视频
慕课网APP