• c++ unordered_map 哈希表使用


    1. find()函数

    C++中的 unordered_map使用时通过键值对进行插入,并通过find(key)进行寻找键值是否存在。

    //iterator find (key) : 找到则返回对应键值得迭代器
    //没有找到则返回unordered_map::end.
    // unordered_map::find
    
      if ( got == mymap.end() )
        std::cout << "not found";
      else
        std::cout << got->first << " is " << got->second;

    不能插入重复元素,即具有同样键值(key)的元素。

    #include <iostream>
    #include <string>
    #include <unordered_map>
    using namespace std;
    
    int main()
    {
        unordered_map<string, string> mymap = {
            {"house", "maison"},
            {"apple", "pomme"},
            {"apple", "pomme"},
            {"apple", "pomme1"},
            {"tree", "arbre"},
            {"book", "livre"},
            {"door", "porte"},
            {"grapefruit", "pamplemousse"}
        };
        unsigned n = mymap.bucket_count();
    
        cout << "mymap has "  << n << "buckets.
    ";
        for(unsigned i = 0; i < n; i++)
        {
            cout << "bucket #" << i  << "contains: ";
            for(auto it = mymap.begin(i); it != mymap.end(i); ++it)
            {
                cout << "[" << it->first << ":" << it->second << "]";
            }
            cout << "
    ";
        } 
        return 0;
    
    }
    /* 输出
    *mymap has 11buckets.
    *bucket #0contains: 
    *bucket #1contains: [book:livre]
    *bucket #2contains: [grapefruit:pamplemousse]
    *bucket #3contains: [house:maison]
    *bucket #4contains: 
    *bucket #5contains: 
    *bucket #6contains: 
    *bucket #7contains: 
    *bucket #8contains: 
    *bucket #9contains: [apple:pomme]
    *bucket #10contains: [door:porte][tree:arbre]
    
     */

     2. 自己写的一个类测试unordered_map的使用,感觉键值映射的value值好像没什么用。

    /*typedef pair<const Key, T> value_type;
     *unordered_map<Key, T>::iterator it;
     (*it).first; | it->first //the key value 
     *(*it).second;| it->second  //the mapped value
     */
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <unordered_map>
    #include <string>
    #include <ctime>
    using namespace std;
    /* compute the number of target value t int the interval[-10000,10000]
     * such that distinct number x, y int the input file that 
     * satisfy x+y=t.
     * 
     */
    class TwoSum
    {
    public:
    
        TwoSum()
        {
            get_data();
            for(long long  x = -10000; x <= 10000; x++)
            {
                calc_times(x);
            }
            
        }
    /************读入数据 ***********/
        void get_data()
        {
            ifstream fin("algo1-programming_prob-2sum.txt");
            string line;
            long long  count = 0;
            stringstream stream;
            while(getline(fin,line))
            {
                long long data;
                stream.clear();
                stream << line;
                stream >> data;
    /* 插入键值对,感觉映射的value有时候没有什么用 */
                map.insert(make_pair( data,count ));    
                count ++;
            }    
        }
    /****************计算x+y=sum对的个数***************/
        void calc_times(long long  sum)
        {
            for(auto &x : map)
            {
                unordered_map<long long, long long >::iterator got = map.find(sum-(x.first));
                if( ( got != map.end() ) && (sum-x.first != x.first) )
                {
                    totalSum ++;
                }
            }
        
        }
    
    
    public:
        unordered_map<long long , long long > map; //hashtable 
        long long totalSum = 0; 
    
    };
    
    int main()
    {
        clock_t start, end;
        start = clock();
        TwoSum twosum;
        end = clock();
        cout << twosum.totalSum << endl;
        cout << "running time:" << (double)(end-start) / CLOCKS_PER_SEC  << "s" << endl;
        return 0;
    
    }
    The Safest Way to Get what you Want is to Try and Deserve What you Want.
  • 相关阅读:
    作业2(5)
    作业2(7)
    作业2(4)
    作业2(3)
    作业2(1)
    作业3(5)
    作业3(6)
    实验7综合练习(4)
    实验7综合练习(2)
    计算成绩
  • 原文地址:https://www.cnblogs.com/Shinered/p/9061696.html
Copyright © 2020-2023  润新知