10--STL無序容器(Unordered Containers)
阿新 • • 發佈:2019-05-01
cli one stdio.h cond elements info tdi target alt
一:無序容器簡介
Unordered Containers也是一種關聯式容器。其中元素是分散,沒有定性的排列(不是圖中那樣松散)。其中元素可能在某一次操作後改變原來的位置。
哈希表的鏈地址法,更能表現其內部實現結構。其中哈希表中表長可以改變,其實現用分配器實現,
為了防止鏈表過程,效率減低,設置一個值,當鏈表長度過長時,打散哈希表,重新設置表長,分配位置。
二:性能測試
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <stdio.h> #include公共函數<cstring> #if _MSC_VER #define snprintf _snprintf #endif using namespace std; long get_a_target_long() { /******變量聲明********/ long target = 0; /**********************/ cout << "targer (0~" << RAND_MAX << "):"; cin >> target; return target; }string get_a_target_string() { /******變量聲明********/ long target = 0; char buf[10]; /**********************/ cout << "targer (0~" << RAND_MAX << "):"; cin >> target; snprintf(buf, 10, "%d", target); return string(buf); } //與後面的比較函數中回調參數對應int compareLongs(const void* l1, const void* l2) { return (*(long*)l1 - *(long*)l2); } int compareStrings(const void* s1, const void* s2) { if (*(string*)s1 > *(string*)s2) return 1; if (*(string*)s1 < *(string*)s2) return -1; return 0; }
(一)unordered_multiset測試
#include <unordered_set> //測試unordered_multiset-->元素可以重復 namespace jj12 { void test_unordered_multiset(long& us_size) { cout << "\ntest_unordered_multiset()*******" << endl; /******變量聲明:數組初始********/ char buf[10]; /******變量聲明:unordered_multiset初始********/ unordered_multiset<string> ums; /******變量聲明:記錄時間********/ clock_t timeStart = clock(); //開始時間 for (long i = 0; i < us_size; i++) { try { snprintf(buf, 10, "%d", rand()); ums.insert(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //終止 } } cout << "inti unordered_multiset use milli-seconds:" << (clock() - timeStart) << endl; //獲取初始化數組耗時 cout << "unordered_multiset.size:" << ums.size() << endl; //獲取unordered_multiset大小 cout << "unordered_multiset.max_size:" << ums.max_size() << endl; //獲取unordered_multiset允許最大長度 cout << "unordered_multiset.bucket_count:" << ums.bucket_count() << endl; //獲取籃子數--表長 cout << "unordered_multiset.load_factor:" << ums.load_factor() << endl; //獲取加載因子 cout << "unordered_multiset.max_load_factoe:" << ums.max_load_factor() << endl; //獲取最大加載因子--1 cout << "unordered_multiset.max_bucket_count:" << ums.max_bucket_count() << endl; //獲取存在最大籃子數--表長 //打印前20個籃子 for (int i = 0; i < 20; i++) cout << "Key #" << i << " has " << ums.bucket_size(i) //該籃子中有幾個元素 << " elements" << endl; /******變量聲明:獲取我們要查詢的數********/ string target = get_a_target_string(); //使用::find方法進行查找 timeStart = clock(); auto pI = find(ums.begin(), ums.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (pI != ums.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; //使用unordered_multiset.find查找 timeStart = clock(); pI = ums.find(target); //比::find塊得多,直接定位查詢, cout << "unordered_multiset.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != ums.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; } }
(二)unordered_multimap測試
#include <unordered_map> //測試unordered_multimap-->元素可以重復 namespace jj13 { void test_unordered_multimap(long& mm_size) { cout << "\ntest_unordered_multimap()*******" << endl; /******變量聲明:數組初始********/ char buf[10]; /******變量聲明:unordered_multimap初始********/ unordered_multimap<long, string> umm; /******變量聲明:記錄時間********/ clock_t timeStart = clock(); //開始時間 for (long i = 0; i < mm_size; i++) { try { snprintf(buf, 10, "%d", rand()); umm.insert(pair<long, string>(i, string(buf))); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //終止 } } cout << "inti unordered_multimap use milli-seconds:" << (clock() - timeStart) << endl; //獲取初始化數組耗時 cout << "unordered_multimap.size:" << umm.size() << endl; //獲取unordered_multimap大小 cout << "unordered_multimap.max_size:" << umm.max_size() << endl; //獲取unordered_multimap所允許最大 cout << "unordered_multimap.bucket_count:" << umm.bucket_count() << endl; //獲取籃子數--表長 cout << "unordered_multimap.load_factor:" << umm.load_factor() << endl; //獲取加載因子 cout << "unordered_multimap.max_load_factoe:" << umm.max_load_factor() << endl; //獲取最大加載因子--1 cout << "unordered_multimap.max_bucket_count:" << umm.max_bucket_count() << endl; //獲取存在最大籃子數--表長 //打印前20個籃子 for (int i = 0; i < 20; i++) cout << "Key #" << i << " has " << umm.bucket_size(i) //該籃子中有幾個元素 << " elements" << endl; /******變量聲明:獲取我們要查詢的數********/ long target = get_a_target_long(); //根據key查找 //unordered_multimap沒有全局::find方法可用,::find找值,multimap找鍵,兩者不同,不可以混用 //使用unordered_multimap.find查找 timeStart = clock(); auto pI = umm.find(target); cout << "unordered_multimap.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != umm.end()) cout << "found:" << (*pI).first << ":" << (*pI).second << endl; else cout << "not found!" << endl; } }
(三)unordered_set測試
#include <unordered_set> //測試unordered_set-->元素不可以重復 namespace jj14 { void test_unordered_set(long& us_size) { cout << "\ntest_unordered_set()*******" << endl; /******變量聲明:數組初始********/ char buf[10]; /******變量聲明:unordered_set初始********/ unordered_set<string> us; /******變量聲明:記錄時間********/ clock_t timeStart = clock(); //開始時間 for (long i = 0; i < us_size; i++) { try { snprintf(buf, 10, "%d", rand()); us.insert(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //終止 } } cout << "inti unordered_multiset use milli-seconds:" << (clock() - timeStart) << endl; //獲取初始化數組耗時 cout << "unordered_set.size:" << us.size() << endl; //獲取unordered_set大小 cout << "unordered_set.max_size:" << us.max_size() << endl; //獲取unordered_set允許最大 cout << "unordered_set.bucket_count:" << us.bucket_count() << endl; //獲取籃子數--表長 cout << "unordered_set.load_factor:" << us.load_factor() << endl; //獲取加載因子 cout << "unordered_set.max_load_factoe:" << us.max_load_factor() << endl; //獲取最大加載因子--1 cout << "unordered_set.max_bucket_count:" << us.max_bucket_count() << endl; //獲取存在最大籃子數--表長 //打印前20個籃子 for (int i = 0; i < 20; i++) cout << "Key #" << i << " has " << us.bucket_size(i) //該籃子中有幾個元素 << " elements" << endl; /******變量聲明:獲取我們要查詢的數********/ string target = get_a_target_string(); //使用::find方法進行查找 timeStart = clock(); auto pI = find(us.begin(), us.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (pI != us.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; //使用unordered_set.find查找 timeStart = clock(); pI = us.find(target); //比::find塊得多,直接定位查詢, cout << "unordered_set.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != us.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; } }
(四)unordered_map測試
#include <unordered_map> //測試unordered_map-->元素不可以重復 namespace jj15 { void test_unordered_map(long& um_size) { cout << "\ntest_unordered_map()*******" << endl; /******變量聲明:數組初始********/ char buf[10]; /******變量聲明:unordered_multimap初始********/ unordered_map<long, string> um; /******變量聲明:記錄時間********/ clock_t timeStart = clock(); //開始時間 for (long i = 0; i < um_size; i++) { try { snprintf(buf, 10, "%d", rand()); um.insert(pair<long, string>(i, string(buf))); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //終止 } } cout << "inti unordered_map use milli-seconds:" << (clock() - timeStart) << endl; //獲取初始化數組耗時 cout << "unordered_map.size:" << um.size() << endl; //獲取unordered_map大小 cout << "unordered_map.max_size:" << um.max_size() << endl; //獲取unordered_map允許最大 cout << "unordered_map.bucket_count:" << um.bucket_count() << endl; //獲取籃子數--表長 cout << "unordered_map.load_factor:" << um.load_factor() << endl; //獲取加載因子 cout << "unordered_map.max_load_factoe:" << um.max_load_factor() << endl; //獲取最大加載因子--1 cout << "unordered_map.max_bucket_count:" << um.max_bucket_count() << endl; //獲取存在最大籃子數--表長 //打印前20個籃子 for (int i = 0; i < 20; i++) cout << "Key #" << i << " has " << um.bucket_size(i) //該籃子中有幾個元素 << " elements" << endl; /******變量聲明:獲取我們要查詢的數********/ long target = get_a_target_long(); //根據key查找 //unordered_map沒有全局::find方法可用,::find找值,unordered_map找鍵,兩者不同,不可以混用 //使用unordered_map.find查找 timeStart = clock(); auto pI = um.find(target); cout << "unordered_map.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != um.end()) cout << "found:" << (*pI).first << ":" << (*pI).second << endl; else cout << "not found!" << endl; } }
10--STL無序容器(Unordered Containers)