关于set和map的区别前面已经说过,这里仅是用hashtable将其实现,所以不做过多说明,直接看程序

unordered_set

#include<stdexcept>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<unordered_set>
using namespace std;
long get_a_target_long()
{
long target = 0;
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
return target;
}
string get_a_target_string()
{
long target = 0;
char buf[10];
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
snprintf(buf, 10, "%ld", target);
return string(buf);
}
int compareLongs(const void* a, const void* b)
{
return (*(long*)a - *(long*)b);
} int compareStrings(const void *a, const void *b)
{
if(*(string*)a > *(string*)b)
return 1;
else if(*(string*)a < *(string*)b)
return -1;
else
return 0;
}
void test_unordered_set(long& value)
{
cout << "\ntest_unordered_set().......... \n"; unordered_set<string> c;
char buf[10]; clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, "%d", rand());
c.insert(string(buf));
}
catch(exception& p) {
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "unordered_set.size()= " << c.size() << endl; //元素个数
cout << "unordered_set.max_size()= " << c.max_size() << endl; //
cout << "unordered_set.bucket_count()= " << c.bucket_count() << endl;//篮子个数
cout << "unordered_set.load_factor()= " << c.load_factor() << endl; //负载
cout << "unordered_set.max_load_factor()= " << c.max_load_factor() << endl;//最大负载
cout << "unordered_set.max_bucket_count()= " << c.max_bucket_count() << endl; //
for (unsigned i=0; i< 20; ++i) {
cout << "bucket #" << i << " has " << c.bucket_size(i) << " elements.\n";
} string target = get_a_target_string();
{
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target); //比 c.find(...) 慢很多
cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
} {
timeStart = clock();
auto pItem = c.find(target); //比 std::find(...) 快很多
cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
}
int main()
{
long int value;
cout<<"how many elements: ";
cin>>value;
test_unordered_set(value);
return 0;
}

运行结果

unordered_map

#include<stdexcept>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<unordered_map>
using namespace std;
long get_a_target_long()
{
long target = 0;
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
return target;
}
string get_a_target_string()
{
long target = 0;
char buf[10];
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
snprintf(buf, 10, "%ld", target);
return string(buf);
}
int compareLongs(const void* a, const void* b)
{
return (*(long*)a - *(long*)b);
} int compareStrings(const void *a, const void *b)
{
if(*(string*)a > *(string*)b)
return 1;
else if(*(string*)a < *(string*)b)
return -1;
else
return 0;
}
void test_unordered_map(long& value)
{
cout << "\ntest_unordered_map().......... \n"; unordered_map<long, string> c;
char buf[10]; clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, "%d", rand());
c[i] = string(buf); //特殊的插入方式
}
catch(exception& p) {
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "unordered_map.size()= " << c.size() << endl; //元素个数
cout << "unordered_map.max_size()= " << c.max_size() << endl; long target = get_a_target_long();
timeStart = clock(); auto pItem = c.find(target);//map 不用 std::find() cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, value=" << (*pItem).second << endl;
else
cout << "not found! " << endl;
}
int main()
{
long int value;
cout<<"how many elements: ";
cin>>value;
test_unordered_map(value);
return 0;
}

运行结果

最新文章

  1. UITextField set placeholderColor and UITextField set clearButton Image
  2. SpringMVC学习记录2
  3. ElasticSearch集群设置
  4. 谈谈vertical-align的text-bottom和text-top - 韦奕
  5. 【转】轻松搞定FTP之FlashFxp全攻略
  6. Qt之QHeaderView自定义排序(获取正确的QModelIndex)
  7. ACM-经典DP之Monkey and Banana——hdu1069
  8. iOS 检测更新版本
  9. CodeChef Sereja and Game [DP 概率 博弈论]
  10. python3 元组tuple
  11. telegram即时通信软件和outline ---- by 余弦 by倾旋
  12. 中国标准时间转换成YYY-MM-DD
  13. leetcode146
  14. ppt 制作圆角三角形
  15. Java实现后缀表达式建立表达式树
  16. AForge.NET简介
  17. 关于python 的空的__init__.py文件的作用,可不可以删除,到底有没有用?
  18. linux安装应用程序
  19. epoll 系列函数简介、与select、poll 的区别
  20. 转 Maven常用仓库地址以及手动添加jar包到仓库

热门文章

  1. Go 日常开发常备第三方库和工具
  2. js实现日期格式化封装-八种格式
  3. 用python写一个自动化盲注脚本
  4. LeetCode刷题 DFS+回溯
  5. macos proxy_bypass_macosx_sysconf exception
  6. SVN错误:Attempted to lock an already-locked dir svn: Working copy locked
  7. Java String 转成 二位数组
  8. [cf1515G]Phoenix and Odometers
  9. [bzoj4557]侦察守卫
  10. generator函数与async/await