容器共性机制研究
一:理论提高
所有容器提供的都是值(value)语意,而非引用(reference)语意。
容器执行插入元素的操作时,内部实施拷贝动作。所以STL容器内存储的元素必须能够被拷贝(必须提供拷贝构造函数)。
容器中的所有元素必须独立于我们提供的数据,防止出现引用。
二:STL容器特点
(一)除了queue与stack外,每个容器都提供可返回迭代器的函数,运用返回的迭代器就可以访问元素。
(二)通常STL不会丢出异常。要求使用者确保传入正确的参数。
(三)每个容器都提供了一个默认构造函数跟一个默认拷贝构造函数。
三:各个容器使用时机
(一)deque使用时机
deque的使用场景:比如排队购票系统,对排队者的存储可以采用deque,支持头端的快速移除,尾端的快速添加。
如果采用vector,则头端移除时,会移动大量的数据,速度慢。
(二)vector和deque比较
一:vector.at()比deque.at()效率高,比如vector.at(0)是固定的,deque的开始位置却是不固定的。 二:如果有大量释放操作的话,vector花的时间更少,这跟二者的内部实现有关。 三:deque支持头部的快速插入与快速移除,这是deque的优点。
(三)list使用场景
list的使用场景:比如公交车乘客的存储,随时可能有乘客下车,支持频繁的不确实位置元素的移除插入。
(四)set使用场景
set的使用场景:比如对手机游戏的个人得分记录的存储,存储要求从高分到低分的顺序排列
(五)map使用场景
map的使用场景:比如按ID号存储十万个用户,想要快速要通过ID查找对应的用户。
二叉树的查找效率,这时就体现出来了。
如果是vector容器,最坏的情况下可能要遍历完整个容器才能找到该用户。
(六)容器性能测试全部代码
#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; } /************************************************************************/ /*测试数组 */ /************************************************************************/ #include <array> #include <iostream> #include <algorithm> #include <ctime> #include <cstdlib> #define ASIZE 500000 //由于数组声明需要使用常量值,故在这里写死 //500000个long---4000000Byte---4M //数组Array测试 namespace jj01 { void test_array() { cout << "\ntest_array()*******" << endl; /******变量声明:数组初始********/ array<long, ASIZE> arr; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < ASIZE; i++) arr[i] = rand(); //0-32767,所以有重复 cout << "inti array use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "array.size:" << arr.size() << endl; //获取数组大小 cout << "array.front:" << arr.front() << endl; //获取数组首元素 cout << "array.back:" << arr.back() << endl; //获取数组尾元素 cout << "array.data:" << arr.data() << endl; //获取数组首地址 /******变量声明:获取我们要查询的数********/ long target = get_a_target_long(); timeStart = clock(); //qsort和bsearch是C编译器自带的快速排序和二分查找算法 qsort(arr.data(), ASIZE, sizeof(long), compareLongs); /******变量声明:pItem是我们获取的返回的元素地址********/ long* pItem = (long*)bsearch(&target, arr.data(), ASIZE, sizeof(long), compareLongs); cout << "qsort()+bsearch(),milli-seconds:" << clock() - timeStart << endl; if (pItem != NULL) cout << "found:" << *pItem << endl; else cout << "not found!" << endl; random_shuffle(arr.begin(), arr.end()); //乱序 //STL排序查找算法 timeStart = clock(); sort(arr.begin(), arr.end()); /******变量声明:flag布尔型判断是否找到数据********/ bool flag = binary_search(arr.begin(), arr.end(), target); cout << "sort()+binary_search(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << *pItem << endl; else cout << "not found!" << endl; //使用find方法进行查找 timeStart = clock(); auto pI = find(arr.begin(), arr.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << *pI << endl; else cout << "not found!" << endl; } } #include <vector> #include <string> //容器vector测试 namespace jj02 { void test_vector(long& v_size) { cout << "\ntest_vector()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:vector初始********/ vector<string> vec; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < v_size; i++) { try { snprintf(buf, 10, "%d", rand()); vec.push_back(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti vector use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "vector.size:" << vec.size() << endl; //获取vector大小 cout << "vector.front:" << vec.front() << endl; //获取vector首元素 cout << "vector.back:" << vec.back() << endl; //获取vector尾元素 cout << "vector.data:" << vec.data() << endl; //获取vector首地址 cout << "vector.capacity:" << vec.capacity() << endl; //获取vector容量 /******变量声明:获取我们要查询的数********/ string target = get_a_target_string(); //STL排序查找算法 timeStart = clock(); sort(vec.begin(), vec.end()); /******变量声明:flag布尔型判断是否找到数据********/ bool flag = binary_search(vec.begin(), vec.end(), target); cout << "sort()+binary_search(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << target << endl; else cout << "not found!" << endl; random_shuffle(vec.begin(), vec.end()); //乱序 timeStart = clock(); //qsort和bsearch是C编译器自带的快速排序和二分查找算法 qsort(vec.data(), vec.size(), sizeof(string), compareStrings); /******变量声明:pItem是我们获取的返回的元素地址********/ string* pItem = (string*)bsearch(&target, vec.data(), vec.size(), sizeof(string), compareStrings); cout << "qsort()+bsearch(),milli-seconds:" << clock() - timeStart << endl; if (pItem != NULL) cout << "found:" << *pItem << endl; else cout << "not found!" << endl; //使用find方法进行查找 timeStart = clock(); auto pI = find(vec.begin(), vec.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << *pI << endl; else cout << "not found!" << endl; } } #include <list> namespace jj03 { void test_list(long& l_size) { cout << "\ntest_list()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:list初始********/ list<string> lt; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < l_size; i++) { try { snprintf(buf, 10, "%d", rand()); lt.push_back(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti vector use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "list.size:" << lt.size() << endl; //获取list大小 cout << "list.max_size:" << lt.max_size() << endl; //获取list所允许的最大长度 cout << "list.front:" << lt.front() << endl; //获取list首元素 cout << "list.back:" << lt.back() << endl; //获取list尾元素 /******变量声明:获取我们要查询的数********/ string target = get_a_target_string(); //注意list是链表实现,算法中sort不能使用,sort用在顺序表中 //qsort和bsearch是C编译器自带的快速排序和二分查找算法 //但是list不维护data首地址(一直变),所以这个查找方法不能使用 //使用find方法进行查找:find不需要进行排序,查找时间可能较短<因为是迭代查找>(若是一次查找的话,耗时少,但是多次查找,那么先排序效率更好) timeStart = clock(); auto pItem = find(lt.begin(), lt.end(), target); //auto变量用得好 cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (pItem!=lt.end()) cout << "found:" << *pItem << endl; else cout << "not found!" << endl; //使用list自带sort方法进行排序,节省时间 需要排序,所以耗时长于find timeStart = clock(); lt.sort(); //使用list自带sort方法进行排序,节省时间 cout << "list.sort(),milli-seconds:" << clock() - timeStart << endl; timeStart = clock(); /******变量声明:flag布尔型判断是否找到数据********/ bool flag = binary_search(lt.begin(), lt.end(), target); cout << "binary_search(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << target << endl; else cout << "not found!" << endl; } } #include <forward_list> namespace jj04 { void test_flist(long& l_size) { cout << "\ntest_list()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:forward_list初始********/ forward_list<string> fl; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < l_size; i++) { try { snprintf(buf, 10, "%d", rand()); fl.push_front(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti vector use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "list.max_size:" << fl.max_size() << endl; //获取forward_list所允许的最大长度 cout << "list.front:" << fl.front() << endl; //获取forward_list首元素 //为了效率,forward_list并不维护size和back /******变量声明:获取我们要查询的数********/ string target = get_a_target_string(); //注意forward_list是链表实现,算法中sort不能使用,sort用在顺序表中 //qsort和bsearch是C编译器自带的快速排序和二分查找算法 //但是forward_list不维护data首地址(一直变),所以这个查找方法不能使用 //使用find方法进行查找:find不需要进行排序,查找时间可能较短<因为是迭代查找>(若是一次查找的话,耗时少,但是多次查找,那么先排序效率更好) timeStart = clock(); auto pItem = find(fl.begin(), fl.end(), target); //auto变量用得好 cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (pItem != fl.end()) cout << "found:" << *pItem << endl; else cout << "not found!" << endl; //使用forward_list自带sort方法进行排序,节省时间 需要排序,所以耗时长于find timeStart = clock(); fl.sort(); //使用list自带sort方法进行排序,节省时间 /******变量声明:flag布尔型判断是否找到数据********/ cout << "list.sort()milli-seconds:" << clock() - timeStart << endl; timeStart = clock(); bool flag = binary_search(fl.begin(), fl.end(), target); cout << "binary_search(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << target << endl; else cout << "not found!" << endl; } } #include <deque> namespace jj05 { void test_deque(long& d_size) { cout << "\ntest_deque()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:vector初始********/ deque<string> deq; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < d_size; i++) { try { snprintf(buf, 10, "%d", rand()); deq.push_back(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti deque use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "deque.size:" << deq.size() << endl; //获取vector大小 cout << "deque.max_size:" << deq.max_size() << endl; //获取vector尾元素 cout << "deque.front:" << deq.front() << endl; //获取vector首元素 cout << "deque.back:" << deq.back() << endl; //获取vector尾元素 /******变量声明:获取我们要查询的数********/ string target = get_a_target_string(); //STL排序查找算法 timeStart = clock(); sort(deq.begin(), deq.end()); /******变量声明:flag布尔型判断是否找到数据********/ bool flag = binary_search(deq.begin(), deq.end(), target); cout << "sort()+binary_search(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << target << endl; else cout << "not found!" << endl; random_shuffle(deq.begin(), deq.end()); //乱序 //使用find方法进行查找 timeStart = clock(); auto pI = find(deq.begin(), deq.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (flag != false) cout << "found:" << *pI << endl; else cout << "not found!" << endl; //等待一段时间释放,因为在destroy中 } } #include <stack> namespace jj06 { void test_stack(long& s_size) { cout << "\ntest_stack()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:vector初始********/ stack<string> stk; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < s_size; i++) { try { snprintf(buf, 10, "%d", rand()); stk.push(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti stack use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "stack.size:" << stk.size() << endl; //获取stack大小 cout << "stack.top:" << stk.top() << endl; //获取stack栈顶元素 stk.pop(); //出栈一个元素 cout << "stack.size:" << stk.size() << endl; //获取stack大小 cout << "stack.top:" << stk.top() << endl; //获取栈顶尾元素 } } #include <queue> namespace jj07 { void test_queue(long& q_size) { cout << "\ntest_queue()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:vector初始********/ queue<string> que; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < q_size; i++) { try { snprintf(buf, 10, "%d", rand()); que.push(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti queue use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "queue.size:" << que.size() << endl; //获取stack大小 cout << "queue.front:" << que.front() << endl; //获取stack栈顶元素 cout << "queue.back:" << que.back() << endl; //获取stack栈顶元素 que.pop(); //出栈一个元素 cout << "queue.size:" << que.size() << endl; //获取stack大小 cout << "queue.top:" << que.front() << endl; //获取栈顶尾元素 cout << "queue.back:" << que.back() << endl; //获取stack栈顶元素 } } #include <set> //测试muliset-->元素可以重复 namespace jj08 { void test_multiset(long& ms_size) { cout << "\ntest_multiset()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:vector初始********/ multiset<string> ms; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < ms_size; i++) { try { snprintf(buf, 10, "%d", rand()); ms.insert(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti multiset use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "multiset.size:" << ms.size() << endl; //获取multiset大小 cout << "multiset.max_size:" << ms.max_size() << endl; //获取multiset所允许最大长度 /******变量声明:获取我们要查询的数********/ string target = get_a_target_string(); //使用::find方法进行查找 timeStart = clock(); auto pI = find(ms.begin(), ms.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (pI != ms.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; //使用muliset.find查找 timeStart = clock(); pI = ms.find(target); cout << "muliset.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != ms.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; } } #include <map> //测试multimap-->元素可以重复 namespace jj09 { void test_multimap(long& mm_size) { cout << "\ntest_multimap()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:multimap初始********/ multimap<long,string> mm; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < mm_size; i++) { try { snprintf(buf, 10, "%d", rand()); mm.insert(pair<long,string>(i,string(buf))); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti multimap use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "multimap.size:" << mm.size() << endl; //获取multimap大小 cout << "multimap.max_size:" << mm.max_size() << endl; //获取multimap所允许最大长度 /******变量声明:获取我们要查询的数********/ long target = get_a_target_long(); //根据key查找 //multimap没有全局::find方法可用,::find找值,multimap找键,两者不同,不可以混用 //使用muliset.find查找 timeStart = clock(); auto pI = mm.find(target); cout << "multimap.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != mm.end()) cout << "found:" << (*pI).first<<":" << (*pI).second << endl; else cout << "not found!" << endl; } } #include <set> //测试set-->元素不可以重复 namespace jj10 { void test_set(long& s_size) { cout << "\ntest_set()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:set初始********/ set<string> s; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < s_size; i++) { try { snprintf(buf, 10, "%d", rand()); s.insert(string(buf)); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti set use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "set.size:" << s.size() << endl; //获取set大小 cout << "set.max_size:" << s.max_size() << endl; //获取set所允许最大长度 /******变量声明:获取我们要查询的数********/ string target = get_a_target_string(); //使用::find方法进行查找 timeStart = clock(); auto pI = find(s.begin(), s.end(), target); cout << "::find(),milli-seconds:" << clock() - timeStart << endl; if (pI != s.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; //使用muliset.find查找 timeStart = clock(); pI = s.find(target); cout << "set.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != s.end()) cout << "found:" << *pI << endl; else cout << "not found!" << endl; } } #include <map> //测试map-->元素不可以重复 namespace jj11 { void test_map(long& m_size) { cout << "\ntest_map()*******" << endl; /******变量声明:数组初始********/ char buf[10]; /******变量声明:map初始********/ map<long, string> m; /******变量声明:记录时间********/ clock_t timeStart = clock(); //开始时间 for (long i = 0; i < m_size; i++) { try { snprintf(buf, 10, "%d", rand()); m.insert(pair<long, string>(i, string(buf))); } catch (exception& e) { cout << e.what() << endl; cout << "Max_size:" << i << endl; abort(); //终止 } } cout << "inti map use milli-seconds:" << (clock() - timeStart) << endl; //获取初始化数组耗时 cout << "map.size:" << m.size() << endl; //获取map大小 cout << "map.max_size:" << m.max_size() << endl; //获取map允许最大长度 /******变量声明:获取我们要查询的数********/ long target = get_a_target_long(); //根据key查找 //map没有全局::find方法可用,::find找值,map找键,两者不同,不可以混用 //使用map.find查找 timeStart = clock(); auto pI = m.find(target); cout << "map.find(),milli-seconds:" << clock() - timeStart << endl; if (pI != m.end()) cout << "found:" << (*pI).first << ":" << (*pI).second << endl; else cout << "not found!" << endl; } } #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; } } #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; } } #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; } } #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; } } void main() { /******变量声明:choose用于选择所测试的容器********/ int choose; cout << "select:"; cin >> choose; /******变量声明:size用于创建容器大小********/ long size; cout << "create size:"; cin >> size; switch (choose) { case 1: jj01::test_array(); break; case 2: jj02::test_vector(size); break; case 3: jj03::test_list(size); break; case 4: jj04::test_flist(size); break; case 5: jj05::test_deque(size); break; case 6: jj06::test_stack(size); break; case 7: jj07::test_queue(size); break; case 8: jj08::test_multiset(size); break; case 9: jj09::test_multimap(size); break; case 10: jj10::test_set(size); break; case 11: jj11::test_map(size); break; case 12: jj12::test_unordered_multiset(size); break; case 13: jj13::test_unordered_multimap(size); break; case 14: jj14::test_unordered_set(size); break; case 15: jj15::test_unordered_map(size); break; default: break; } system("pause"); }