• C++回调函数


     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 
     5 
     6 void SelectSort( int *pArray, int size ){
     7     if( size <= 1 )
     8     {
     9         return;
    10     }
    11 
    12     for( int i=0; i<size-1; ++i )
    13     {
    14         int min = i;
    15         for( int j=i+1; j<size; ++j )
    16         {
    17             if( pArray[j] < pArray[min] )
    18             {
    19                 min = j;
    20             }
    21         }
    22         if( min != i )
    23         {
    24             int temp = pArray[i];
    25             pArray[i] = pArray[min];
    26             pArray[min] = temp;
    27         }
    28     }
    29 
    30 }
    31 
    32 template< typename T >
    33 void Display( T *pArray, int size )
    34 {
    35     for( int i=0; i<size; ++i )
    36     {
    37         cout.width(4);
    38         cout.setf( ios::left );
    39         cout << pArray[i];
    40     }
    41     cout << endl;
    42 }
    43 
    44 
    45 void Display1( const string &str )
    46 {
    47     cout << str << " is very high" << endl; 
    48 }
    49 
    50 void Display2( const string &str )
    51 {
    52     cout << str << " is very rich" << endl; 
    53 }
    54 
    55 void Display3( const string &str )
    56 {
    57     cout << str << " is very handsome" << endl; 
    58 }
    59 
    60 void Show( string str, void (*dis)(const string&) )
    61 {
    62     dis( str );
    63 }
    64 
    65 void ShowStrArray( string *str, int size, void (*dis)(const string&) )
    66 {
    67     if( size <= 0 )
    68     {
    69         return;
    70     }
    71 
    72     for( int i=0; i<size; ++i )
    73     {
    74         dis( str[i] );
    75     }
    76 }
    77 
    78 int main()
    79 {
    80     //cout << "Hello" << endl;
    81 
    82     //int pNum[] = { 2, 1, 5, 3, 4 };
    83     //SelectSort( pNum, 5 );
    84     //Display( pNum, 5 );
    85 
    86     //Show("Green",Display1);
    87     //Show("Kitty",Display2);
    88     //Show("Lisa",Display3);
    89 
    90     string pStr[] = { "Green", "Kitty", "Lisa" };
    91     ShowStrArray( pStr, 3, Display1 );
    92 
    93 
    94 
    95 
    96     ShowStrArray( pStr, 3, Display2 );
    97     ShowStrArray( pStr, 3, Display3 );
    98     return 0;
    99 }

    运行结果:

  • 相关阅读:
    我与计算机
    C高级第四次作业
    C高级第三次作业
    C高级第二次作业
    C高级第一次PTA作业 要求三
    C高级第一次PTA作业
    第0次作业
    # 20182304 实验七 《数据结构与面向对象程序设计》实验报告
    # 20182304 实验八 《数据结构与面向对象程序设计》实验报告
    # 20182304 《数据结构与面向对象程序设计》第八周学习总结
  • 原文地址:https://www.cnblogs.com/MiniHouse/p/3884703.html
Copyright © 2020-2023  润新知