如果原有的代码质量比较好,尽量复用它

如果原有的代码质量比较好,尽量复用它。

但是不要修补很差劲的 代码,应当重新编写。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 
 7 
 8 
 9 using namespace std;
10 //利用类模板生成实例
11 typedef vector < int > IntArray;
12 
13 //显示数组
14 void put_array(int x[],int size) {
15      for(int i=0;i<size;i++)
16         cout<<x[i]<<" ";
17     cout<<endl;
18 }
19 //显示vector容器中的元素
20 void put_vector(IntArray v)
21 {
22     IntArray::iterator theIterator;
23 
24     for (theIterator=v.begin();theIterator!=v.end();++theIterator){
25         cout<<(*theIterator)<<" ";
26     }
27     cout<<endl;
28 }
29 
30 //在main()函数中测试fill和fill_n算法
31 int main(int argc, char** argv) {
32 {
33 //--------------------------------------------
34 //    fill和fill_n算法对普通数组的计算
35 //---------------------------------------------
36     int x[]={1,3,5,7,9};
37     cout << "x[]: ";
38     put_array(x,5);
39     //填数处理
40     fill(x+1,x+3,2);
41     cout << "fill(x+1,x+3,2): "<<endl;
42     put_array(x,5);
43     fill_n(x,3,8);
44     cout << "fill_n(x,3,8): "<<endl;
45     put_array(x,5);
46 //--------------------------------------------
47 //    fill和fill_n算法对于vector容器的计算
48 //---------------------------------------------
49     //声明intvector容器和迭代器ii
50     IntArray intvector;
51 
52     //向intvector容器中插入元素
53     for (int i=1; i<=10; i++) {
54         intvector.push_back(i);
55     };
56     //显示intvector容器中的元素值和统计结果
57     cout << "intvector: "<<endl;
58     put_vector(intvector);
59     //填数处理
60     fill(intvector.begin(),intvector.begin()+3,2);
61     cout << "fill(intvector.begin(),intvector.begin()+3,2): "<<endl;
62     put_vector(intvector);
63     fill_n(&intvector[5],3,8);
64     cout << "fill_n(&intvector[5],3,8): "<<endl;
65     put_vector(intvector);
66     
67         return 0;
68 }
69 }
原文地址:https://www.cnblogs.com/borter/p/9418298.html