如果可能的话,使用 PC-Lint、LogiScope 等工具进行代码审查

如果可能的话,使用 PC-Lint、LogiScope 等工具进行代码审查。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 #include <stdlib.h>
 5 #define ARRAY_SIZE 15
 6 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 7 
 8 
 9 
10 
11 
12 using namespace std;
13 
14 //定义整型数的vector容器类
15 typedef vector<int > IntVector ;
16 
17 //显示数组
18 void put_array(int x[],int size) {
19     for(int i=0;i<size;i++)
20         cout<<x[i]<<" ";
21     cout<<endl;
22 }
23 
24 //显示vector容器中的元素
25 void put_vector(IntVector v,char *name)
26 {
27     IntVector::iterator theIterator;
28     cout<<name<<": ";
29     for (theIterator=v.begin();theIterator!=v.end();++theIterator){
30         cout<<(*theIterator)<<" ";
31     }
32     cout<<endl;
33 }
34 
35 //产生指定范围的整数随机数
36 int getrand(int min,int max) {
37     int m;
38     m=(max-min);
39     m=min+double(rand())/RAND_MAX*m ;
40     return m;
41 }
42 
43 //在main()函数中测试sort()和partial_sort()算法
44 int main(int argc, char** argv) {
45 
46     int i;
47 //--------------------------------------------
48 //    sort()和partial_sort()算法对普通数组处理
49 //---------------------------------------------
50     //sort()算法处理数组,并显示
51     int x[ARRAY_SIZE];
52     for (i=0;i<ARRAY_SIZE;i++) {
53         x[i]=getrand(1,20);
54     }
55     cout<<"x[]:";
56     put_array(x,ARRAY_SIZE);
57     sort(x,x+ARRAY_SIZE);
58     cout<<"sort(x,x+ARRAY_SIZE):"<<endl;
59     put_array(x,ARRAY_SIZE);
60 
61     //partial_sort()算法对于数组进行处理
62     int y[ARRAY_SIZE];
63     for (i=0;i<ARRAY_SIZE;i++) {
64         y[i]=getrand(1,30) ;
65     }
66     cout<<"y[]:";
67     put_array(y,ARRAY_SIZE);
68     partial_sort(y+2,y+7,y+ARRAY_SIZE);
69     cout<<"partial_sort(y+2,y+7,y+ARRAY_SIZE):"<<endl;
70     put_array(y,ARRAY_SIZE);
71 //--------------------------------------------
72 //    sort()和partial_sort()算法对vector容器的处理
73 //---------------------------------------------
74     IntVector Numbers1,Numbers2;
75     for(i=0;i<15;i++) {
76         Numbers1.push_back(getrand(1,30));
77         Numbers2.push_back(getrand(1,30));
78     }
79     put_vector(Numbers1,"Numbers1");
80     put_vector(Numbers2,"Numbers2");
81 
82     //sort()算法处理并显示
83     sort(Numbers1.begin(),Numbers1.end());
84     cout<<"After call sort():"<<endl;
85     put_vector(Numbers1,"Numbers1");
86 
87     //partial_sort()算法处理并显示
88     partial_sort(Numbers2.begin()+2,Numbers2.begin()+7,Numbers2.end());
89      cout<<"After call partial_sort():"<<endl;
90      put_vector(Numbers2,"Numbers2");  
91      
92          return 0;
93 }
原文地址:https://www.cnblogs.com/borter/p/9418317.html