博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++学习之二分查找续
阅读量:4646 次
发布时间:2019-06-09

本文共 3389 字,大约阅读时间需要 11 分钟。

本文主要对上篇博文的 main函数 进行封装。

随机生成数据rand.cc 见上篇博文。

封装为函数及其各自的作用如下:

//读取数据到vecvoid readfile(const string &filename , vector
&vec);//二分查找bool BinarySearch(const vector
&vec , int val);//暴力查找bool SimpleSearch(const vector
&vec , int val) ;//查找数据 并打印出未在白名单里的数据以及这些数据的总和。 int findandprint(const vector
&white , const vector
&data );//程序运行时间int64_t getTime();

 完整代码如下:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 using namespace std ; 12 13 //读取数据到vec 14 void readfile(const string &filename , vector
&vec); 15 //二分查找 16 bool BinarySearch(const vector
&vec , int val); 17 //暴力查找 18 bool SimpleSearch(const vector
&vec , int val) ; 19 //查找数据 并打印出未在白名单里的数据以及这些数据的总和。 20 int findandprint(const vector
&white , const vector
&data ); 21 //程序运行时间 22 int64_t getTime(); 23 24 25 int main(int argc, const char *argv[]) 26 { 27 //读取white数据到文件 28 //读取testcase 数据至文件 29 //对whitecase排序 30 //查找测试数据 31 if(argc < 3) 32 { 33 fprintf(stderr , "Usage :%s , white ,testcase\n", argv[0]); 34 exit(EXIT_FAILURE); 35 } 36 int64_t starttime = getTime(); 37 38 vector
white ; 39 vector
testcase ; 40 string whiteFile = argv[1]; 41 string testFile = argv[2]; 42 43 readfile(whiteFile ,white); 44 readfile(testFile , testcase); 45 46 sort(white.begin() , white.end()); 47 48 int cnt = findandprint(white , testcase); 49 50 int64_t endtime = getTime(); 51 int64_t cost = endtime - starttime ; 52 53 cout << "白名单大小:" << white.size() 54 << "测试数据大小:" << testcase.size() 55 << "未出现在白名单的数据的总和:" << cnt 56 << "花费时间:" << (cost/1000) << " ms" << endl ; 57 return 0; 58 } 59 60 void readfile(const string &filename , vector
&vec) 61 { 62 vec.clear(); 63 FILE* fp = fopen(filename.c_str() , "rb") ; 64 if(NULL == fopen) 65 { 66 fprintf(stderr ,"Usage :%s open failure", filename.c_str()); 67 exit(EXIT_FAILURE); 68 } 69 70 char buf[128]; 71 while(memset(buf ,0,sizeof buf),fgets(buf ,sizeof buf ,fp)!= NULL) 72 { 73 int val = atoi(buf); 74 vec.push_back(val); 75 } 76 fclose(fp); 77 } 78 79 bool BinarySearch(const vector
&vec , int val) 80 { 81 int low = 0 ; 82 int high = vec.size() - 1 ; 83 84 while(low <= high) 85 { 86 int mid = (low + high)/ 2 ; 87 if(val == vec[mid]) 88 return true ; 89 else if(val > vec[mid]) 90 low = mid + 1 ; 91 else 92 high = mid - 1 ; 93 } 94 return false ; 95 } 96 97 bool SimpleSearch(const vector
&vec , int val) 98 { 99 for(vector
::size_type ix = 0 ;ix != vec.size() ; ix++)100 {101 if(vec[ix] == val)102 return true ;103 }104 return false ;105 }106 107 int findandprint(const vector
&white , const vector
&data )108 {109 int cnt = 0 ;110 for(vector
::size_type ix = 0 ; ix != data.size() ; ++ ix)111 {112 if(!BinarySearch(white , data[ix]))113 {114 cout << data[ix] << endl ;115 cnt ++ ;116 }117 }118 return cnt ;119 }120 121 int64_t getTime()122 {123 struct timeval tm ;124 memset(&tm , 0 , sizeof(tm));125 if(gettimeofday(&tm ,NULL) == -1)126 {127 perror("gettimeofday");128 }129 130 int64_t t = 0 ;131 t += tm.tv_sec * 1000 * 1000;132 t += tm.tv_usec ;133 return t ;134 }

本文编译代码:

1 g++ BinarySearch.cc2 3 ./a.out white.txt testcase.txt

 

转载于:https://www.cnblogs.com/xfxu/p/3979168.html

你可能感兴趣的文章
【HANA系列】SAP HANA SQL获取上周的周一
查看>>
对称矩阵
查看>>
轮播图笔记!
查看>>
值类型与引用类型
查看>>
This kernel requires an x86-64 CPU, but only detected an i686 CPU.
查看>>
PAT 1023 Have Fun with Numbers[大数乘法][一般]
查看>>
三维空间中的几种坐标系
查看>>
乘法表
查看>>
4.express 框架
查看>>
Java基础算法集50题
查看>>
Android 桌面组件widget
查看>>
25-字符串
查看>>
萌新报道
查看>>
Asp.Net 获取物理路径
查看>>
Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫...
查看>>
Solr reRankQuery加自定义函数实现搜索二次排序
查看>>
latex学习(四)tlmgr
查看>>
centos6.5 bugzilla4.4.5 汉化
查看>>
ros topic 发布一次可能会接收不到数据
查看>>
字符串的扩展
查看>>