C++day08 學習筆記

來源:互聯網
上載者:User

1、排序函數

   void order(int* p1, int* p2){       if(*p1 > *p2){           int temp = *p1;           *p1 = *p2;           *p2 = temp;       }   }
   void sort (int *p , int len , int perLen , void (*pOrder)(void* , void*)){       char* pt = (char*)p;       for(int i = 0 ; i < len ; i++){            for(int j = i ; j < len ; i ++){                  pOrder(pt+i*perLen , pt+j*perLen);            }       }   }    

   (1)將輸入參數int* -> void*  (void* 可以儲存任何類型的地址,可以通用 )
   (2)表示數組的第i個元素,事先要知道數組每個元素的大小(參數傳入)
         i*perLen => 當不知道指標類型時,表示走一步,應越過的位元組數
         p+i*perLen => 表示第i個元素的地址
   (3)具體的數組要有具體的排序方法
        調用函數,當不知道函數名時,可以通過函數指標調用

View Code

    ==================================================                     sort.h    ==================================================                         void sort( void * p , int len , int perLen ,           void (*pOrder)(void * , void * ) );    ==================================================                     sort.cc    ==================================================    void sort( void * p , int len , int perLen ,                  void (*pOrder)(void * , void * ) ){           char* pt = (char*)p ;           for( int i = 0 ; i < len ; i++ ){                 for( int j = i ; j < len ; j++ ){                      pOrder(pt+i*perLen,pt+j*perLen ) ;                 }           }    }                     
View Code

    ==================================================                     main.cc    ==================================================    #include <iostream>    #include "sort.h"    using namespace std;    void orderInt( void * p1 , void * p2 ){         int * t1 = (int*)p1 ;         int * t2 = (int*)p2 ;         if( *t1 > *t2 ){              int temp = *t1 ;              *t1 = *t2 ;              *t2 = temp ;         }    }    struct Person{         char name[ 40 ] ;         int age ;         int id ;    } ;    void orderByAge( void * p1 , void* p2 ){         Person * t1 = (Person*)p1 ;         Person * t2 = (Person*)p2 ;         if( t1->age > t2->age ){               Person t = *t1 ;               *t1 = *t2 ;               *t2 = t ;         }        }    void orderById( void *p1 , void* p2 ){         Person* t1 = (Person*)p1 ;         Person* t2 = (Person*)p2 ;         if( t1->id > t2->id ){               Person t = *t1 ;               *t1 = *t2 ;               *t2 = t ;         }    }    void orderByName( void * p1 , void* p2 ){          Person* t1 = (Person*)p1 ;          Person* t2 = (Person*)p2 ;          if( strcmp( t1->name , t2->name ) > 0 ){                Person t = *t1 ;                *t1 = *t2 ;                *t2 = t ;              }    }    int main(){          int ia[] = { 3,1,6,3,6,8,3,468,89 };          sort( ia , 9, sizeof(int), orderInt );          for( int i = 0 ; i < 9 ; i++ ){                cout<<ia[i] <<" " ;          }          cout<<endl;          Person pers[ 3 ] ;          pers[0].id = 1 ;          pers[0].age = 29 ;          strcpy( pers[0].name , "liucy" ) ;          pers[1].id = 2 ;          pers[1].age = 28 ;          strcpy( pers[1].name , "huxinzhe" ) ;          pers[2].id = 3 ;          pers[2].age = 26 ;          strcpy( pers[2].name , "xuehailu" ) ;                  sort( pers , 3 , sizeof(Person), orderByAge );          for( int i = 0 ; i < 3 ; i++ ){                cout<<pers[i].name <<","<<pers[i].age<<",";                cout<<pers[i].id<<endl ;          }          sort( pers, 3, sizeof( Person) , orderById ) ;          for( int i = 0 ; i < 3 ; i++ ){                cout<<pers[i].name <<","<<pers[i].age<<",";                cout<<pers[i].id<<endl ;          }          sort( pers , 3 , sizeof( Person ) , orderByName );          for( int i = 0 ; i < 3 ; i++ ){                cout<<pers[i].name <<","<<pers[i].age<<",";                cout<<pers[i].id <<endl;           }                       return 0 ;      }

2、物件導向
   封裝:對象表示
   繼承:更好的代碼重用
   多態
  
   對象的組成 : 屬性   成員變數
                 行為   函數
                
   面向過程的表示方法:
       資料與函數分離,關係鬆散
       封裝的作用,把資料和函數封裝到一起,保證資料專用
      
  全域函數:在類外面的函數,要使用成員變數,要通過參數傳進來
  成員函數:在類內,可直接使用自己類的成員變數

  對於類的變數的初始化:

       Person p ;       strcpy(p.name , "liucy");       p.age = 23;       p.speak();

  對成員變數和成員函數的使用都要通過類的對象
 
  public 關鍵字,表示在其他地方可以使用
  預設是私人的,在main函數中不能使用
 
  成員變數和成員函數依賴於類的對象(執行個體)
 
  類型是對對象的描述
  對象是類型的執行個體
 
  對象自己的成員函數訪問自己的成員變數 
 
  什麼是類?類由什麼組成?
  怎麼使用類?及類和對象的關係?
  成員變數和成員函數歸誰所有?
 
  物件導向的方法寫程式
  (1)首先寫一個類,描述對象
       用變數表示屬性,函數表示行為
  (2)調用函數
       建立一個類的對象,通過對象調用函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.