1. 程式人生 > >如果原有的代碼質量比較好,盡量復用它

如果原有的代碼質量比較好,盡量復用它

fill urn sin input main space pre tar bsp

如果原有的代碼質量比較好,盡量復用它。

但是不要修補很差勁的 代碼,應當重新編寫。

 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 }

如果原有的代碼質量比較好,盡量復用它