C++言語のこまごました知識点のまとめ

18909 ワード

1,ポインタ関数,関数ポインタ,関数ポインタ配列2,カスタム比較関数,構造体,sort関数におけるless演算子とgreater演算子,優先キューのリロード演算子3,malloc 4,C言語におけるビット演算子&|はどのように演算されるか5,c言語データ型の範囲6,p++と++p 7,strlen関数とsizeofオペレータ8,進数変換の方法
  • C言語ポインタ関数、関数ポインタ、関数ポインタ配列1、ポインタ関数ポインタ関数はポインタ値を返す関数であり、本質は関数である.したがって、ポインタ関数は「戻り値がポインタの関数」に等しい.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    #include 
    using namespace std;
    int *GetNum(int x); //        
    void main(void)
    {
        cout << "===============start================" << endl;
        int num;
        cout << "Please enter the number between 0 and 6: ";
        cin >> num;
        cout << "result is:" << *GetNum(num) << endl;    //          
        system("pause");
    }
    
    int *GetNum(int x) {
        static int num[] = { 0,1,2,3,4,5,6 };	//    static   ,           ,         
        return &num[x];  //      
    }
    

    2、関数ポインタ関数ポインタは、関数を指すポインタです.各関数はコンパイル時にエントリアドレスが割り当てられ、一般的に関数名で表され、このアドレスが関数のポインタである.構文: :type (*func)( )上記の定義形式から分かるように、関数ポインタとポインタ関数の直感的な違いは、ポインタ記号*と関数名/ポインタ名が括弧()で包まれているかどうかであり、この点から両者を区別するのは容易である.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    #include 
    using namespace std;
    
    int max(int a, int b) {
        return a>b ? a : b;
    }
    
    void main(void)
    {
        cout << "===========start===========" << endl;
        int(*func)(int, int);       //                
        func = max;
        int a, b;
        cout << "Please enter two numbers:";
        cin >> a >> b;
        cout << "max=" << (*func)(a, b) << endl;    //          
        cout << "max=" << max(a, b) << endl;        //       
        cout << "max=" << func(a, b) << endl;       //         ,func = max
        system("pause");
    }
    

    上記の例では、定義されたポインタ変数(*func)で直接呼び出される2つの最も一般的な呼び出し形式と、ポインタ名で呼び出される2つの呼び出し形式が示されています.後者は、元の関数を新しい名前で呼び出すように見え、関数の別名と見なすことができます.もう一度強調します.ポインタ変数名とポインタ記号*は必ず括弧で包んでください.
    3、関数ポインタ配列は各要素が関数ポインタの配列であり、直接関数ポインタ名の後ろに配列記号[]を付ければよい.構文: :type (*func[ ])( )
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    
    #include
    using namespace std;
    
    void fun1()
    {
        cout << "    fun1" << endl;
    }
    void fun2()
    {
        cout << "    fun2" << endl;
    }
    void fun3()
    {
        cout << "    fun3" << endl;
    }
    int main()
    {
        //        
        void(*pfun)() = &fun1;
        void(*pfun2)() = &fun2;
        void(*pfun3)() = &fun3;
        //                   。
        void(*pfunarr[3])();
        void(*pfunarr[3])();
        pfunarr[0] = pfun;
        pfunarr[1] = pfun2;
        pfunarr[2] = pfun3;
        /*        
        pfunarr[0] = &fun1;
        pfunarr[1] = &fun2;
        pfunarr[2] = &fun3;
        */
        //    
        pfunarr[0]();
        pfunarr[1]();
        pfunarr[2]();
        /*        
        (*pfunarr[0])();
        (*pfunarr[1])();
        (*pfunarr[2])();
        */
        system("pause");
        return 0;
    }
    

    4、関数ポインタ配列へのポインタ構文: :type (* (*func )[ ])( )
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    
    #include
    using namespace std;
    
    void fun1()
    {
        cout << "    fun1" << endl;
    }
    void fun2()
    {
        cout << "    fun2" << endl;
    }
    void fun3()
    {
        cout << "    fun3" << endl;
    }
    int main()
    {
    
        //void(*pfun)() = &fun1;
        //void(*pfun2)() = &fun2;
        //void(*pfun3)() = &fun3;
        //                   。
        void(*pfunarr[3])();
        void(*(*pfunarr2)[3])();
        //           ,       3                   。
        pfunarr[0] = &fun1;
        pfunarr[1] = &fun2;
        pfunarr[2] = &fun3;
    
        pfunarr2 = &pfunarr;
        (*pfunarr2)[0]();
        pfunarr[0]();
        system("pause");
        return 0;
    }
           :void(*(*pfunarr2)[3])()
      (* pfunarr2)[3]      , void(* )( )      ,                   。
    

    5、ポインタ、アドレス、参照参照:参照は変数の別の名前であり、別名とも呼ばれる.定義方法:int a=10; int &b=a;ここでは、a変数に新しい名前bを付けたことを意味するので、bは再定義できない.参照は初期化され、空の参照がなく、参照は等級を問わない必要があります.たとえば、スワップ関数swap()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    
    void swap(int &a,int &b)
    {
       int temp=a;
       a=b;
       b=temp;
    }
    void main()
    {
       int x=10,y=20;
       swap(x,y);
    }
    
         :
    void swap(int *const a,int *const b)
    {
       int tmp=*a;
       *a=*b;
       *b=temp;
    }
    void main()
    {
    int x=10,y=20;
    swap(&x,&y);
    }
    
               
    int pos;
    char ch[10];
    int a[3];
    scanf(%d,&pos);   //    &,       
    scanf(%s,ch);
    scanf(%d,&a[1]);	//  a[3]  a[0],a[1],a[2]    ,  a[3]   
    

    変数aは本質的に記憶ユニットを表す.CPUは、記憶部のアドレスを介して記憶部のデータにアクセスする.従ってaは、本来、メモリセルのアドレスとメモリセルのデータの2つの値を表す.そこで二異性ができた.このような二義性を解消するために、C言語は、aがメモリセル内のデータを表し、&aがメモリセルのアドレスを表すことを規定する.ここで、aに対応するメモリセルのデータは、必ず別のメモリセルのアドレスであることが要求される.定義int× b;int a=5; b=&a;すると、×bは、別のメモリセルにおけるデータを表す.ポインタに値を割り当てるときb=&a;*bについてaに対応するメモリセルのアドレスのデータを示す.&aは、aに対応するメモリセルのアドレスを示す.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    #include
    using namespace std;
    int main(){
    	int* b;
    	int a=5;
    	b=&a;
    	cout<

    6、方法配列要素の変更
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    #include
    using namespace std;
    void xiu(int *a){
    	a[0]=a[1];
    }
    /*
    void xiu(int a[]){
    	a[0]=a[1];
    }
    void xiu(int a[4]){
    	a[0]=a[1];
    }
                s       ,            
    */
    int main(){
    	int s[4]={1,2,3,4};
    	xiu(s);
    	for(int i=0;i<4;i++)
    	cout<

    6、return文return文の一般的な形式は:return式;または:return(式);ありますか、ありませんか.例:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    return max;
    return a+b;
    return (100+200);
    ~~~ 
    
    **return        ,             ,              return      **,         (              ,  Go  )
    
    **       return        ,               。**
    
    **return               。return          ,              ;return            ,        ,        。**
    
    ------
    

    - 1.1、 operator

    struct Edge
    {
    int from, to, weight;
    };
    bool operator{
    //使用大于号实现小于号,表示排序顺序与默认顺序相反。若使用小于号实现小于号,则相同。可用此法方便理解。
    return a.weight > b.weight;
    }

    1
    2
    

    #include
    #include
    #include
    #include
    #include using namespace std;
    struct Edge{ int from, to, weight; Edge(int from, int to, int weight):from(from), to(to), weight(weight){}};
    bool operator{return a.weight>b.weight;//もとは}
    int main(){ priority_queue pq; pq.push(Edge(0, 1, 7)); pq.push(Edge(1, 2, 4)); pq.push(Edge(2, 3, 10)); pq.push(Edge(3, 4, 5));
    //           (less,    ),                  。
    while(!pq.empty())
    {
        cout << pq.top().weight << endl;
        pq.pop();
    }
    
    vector vec;
    vec.push_back(Edge(0, 1, 7));
    vec.push_back(Edge(1, 2, 4));
    vec.push_back(Edge(2, 3, 10));
    vec.push_back(Edge(3, 4, 5));
    sort(vec.begin(), vec.end());
    
    //sort()       ,       。
    for(int i = 0; i < vec.size(); ++i)
    {
        cout << vec[i].weight << endl;
    }
    
    set se;
    set::iterator iter;
    se.insert(Edge(0, 1, 7));
    se.insert(Edge(1, 2, 4));
    se.insert(Edge(2, 3, 10));
    se.insert(Edge(3, 4, 5));
    
    //set     ,       。
    for(iter = se.begin(); iter != se.end(); ++iter)
    {
        cout << iter -> weight << endl;
    }
    return 0;
    

    }対応する出力は:4 5 7 10 10 7 5 4 10 7 4 4
    1
    2
    3
    
    
    2、           :
            :
    

    struct Edge{ int from, to, weight;};bool cmp(Edge a, Edge b){ return a.weight > b.weight;}
    //sort()関数sort(data.begin()、dataを使用します.end(), cmp);
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    
    
      :
    
    ```
    #include 
    #include 
    #include 
    using namespace std;
     
    struct Edge
    {
    	int from, to, weight;
    	Edge(int from, int to, int weight):from(from), to(to), weight(weight){}
    };
     
    bool cmp(Edge a, Edge b)
    {
    	return a.weight > b.weight;
    }
    int main()
    {
    	vector vec;
    	vec.push_back(Edge(0, 1, 7));
    	vec.push_back(Edge(1, 2, 4));
    	vec.push_back(Edge(2, 3, 10));
    	vec.push_back(Edge(3, 4, 5));
    	sort(vec.begin(), vec.end(), cmp);
    	//       ,       。
    	for(int i = 0; i < vec.size(); ++i)
    	{
    		cout << vec[i].weight << endl;
    	}
    	
    	return 0;
    }
    ```
    
    3、   operator()():
    operator()         (  )         。  :
    

    #include
    #include
    #include using namespace std;
    struct cmp{ bool operator()(const int &a, const int &b) { return a > b; }};
    int main(){set se;set::iterator iter;se.insert(7);se.insert(4);se.insert(10);se.insert(5);//sort()関数はデフォルトでインクリメンタルであり、ここでは降順に反転する.for(iter=se.begin();iter!=se.end();++iter){cout<*iter<return 0;
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    
    
        ,      :  
    1、**operator priority_queue  ,         。   sort()            。**  
    2、      ,   STL sort()      。  
    3、**operator()()                (operator()()           )  sort(),           set priority_queue, cmp>,  cmp    operator()()      。
      
    4.      ,sort    less   greater  ,          
    
    
    ```
     、  sotr   : 
    sort            
    1、   bool    1(       ) 
    bool cmp(const int &a,const int &b)
    //    b;//          
    } //                pair      。
       sort      sort(v.begin(),v.end(),cmp);
         ab     ,
                              。
     
     2、   bool    2(       ) 
     struct node{
        int x, y;
    };
    
    struct cmp1{
        bool operator()(node a, node b){
            return a.x > b.x; //    ,            
        }
    }
    
    struct cmp2{
        bool operator()(node a, node b){//  ()     < 
            return a.x < b.x; //    ,          
        }
    }
    
    int main(){
        priority_queue, cmp1> que1; //   
        priority_queue, cmp2> que2; //   
        return 0;
    }
     
     
      、     :
     1、                       , : 
     struct ss
    {
        int a,b;
    };
    bool cmp(const ss &a,const ss &b)
    { 
        return a.ab.a;          
     
     2、      b.x; //       
    } 
    
     、  priority_queue,****     : 
    1、
    priority_queue q;      //   less,      ,      ,               
    priority_queue, greater > q;    //    ,             
    2、     < (             ,              pair       ,
                。) 
    struct node{
        int x, y;
    };
    
    bool operator  b.x;//       false 
    	 //less     ,     ,       ,        ,
    	 //     a.x > b.x(operator   (const node& a, const node& b){
    //    return a.x < b.x //greater     ,     (    ) 
    //}
    int main(){
        priority_queue, less > que;//  less  operator   , greater > que;//       
        return 0;
    }
    
    3、     < (             ) 
    struct node{
        int x, y;
        bool operator  a.x;//          ,                
        }
        //bool operator >(const node& a) const { //   const
        //    return x < a.x; //         
        //}
    };
    int main(){
        priority_queue, less > que;//     
        //priority_queue, greater > que;//     
        return 0;
    }
    
    
     
    set、map                   priority_queue  。
    set > st; //      ,   less
    typedef pair  P;
    set

    st; // pair , set

    > st;// struct cow{ int x; int y; cow(int x,int y):x(x),y(y){} bool operatorb.x } greater less ( less,sort less, , ) (queue,set ) x y ( less (operator , ) , x > y), , x y bool operator b.x; // , ( ) return a.x < b.x; // , ( ) } bool operator >(const node& a, const node& b){ // operator >, greater return a.x < b.x // , ( ) return a.x > b.x // , ( ) } bool operator a.x;// , } struct cmp1{ bool operator()(node a, node b){// () < return a.x > b.x; // , ( ) return a.x < b.x; // , ( ) } } ( )-less ( ) - ```

    ##### ,c malloc free ![upload successful](\aoyue\images\pasted-93.png) ![upload successful](\aoyue\images\pasted-95.png) ![upload successful](\aoyue\images\pasted-94.png) -----

       , , 。    : ‘B’ : 01111010    , B ASCII , , 。 ** 。** c , 。 c : 1: &( ) — && 2: |( ) — || 3: ^( ) 4: >>( ) 5: < unsigned int 0~42,9496,7295 int( int32, long ) -21,4748,3648~21,4748,3647 (21 1e9) unsigned long 0~42,9496,7295 (42 1e9) long -21,4748,3648~21,4748,3647 long long :922,3372,0368,5477,5807 (1e18) long long :-922,3372,0368,5477,5808 unsigned long long :184,4674,4073,7095,5161 __int64 :922,3372,0368,5477,5807 (1e18) __int64 :-922,3372,0368,5477,5808 unsigned __int64 :1844,6744,0737,0955,1615 __int128 : 128 int , GCC 128 , __int128_t __uint128_t, 。 printf() , myitoa(), 128 。 ------------------------------------------------------------------

    *p++ ++*p


    #include using namespace std;int main(int argc,char argv[]){const char p=“hello”;cout<int a = 6; int *pa = &a; cout << *pa++ << endl; cout << ++*pa << endl; return 0;
    }運転結果は以下の通り:hhl 6-126891224~~~
    解読:p++、(値取り演算子)と++(自増演算子)は同じ第2優先レベルの演算子であるため、それらが変数pに同時に作用すると、右から左への結合順に演算される.したがって、p++は(p++):
        :   p++,    ,p      1(   p          ),     p      。
       :   *(p++),          ,     (p++)       p   ,                  ,       :h。
    

    ++pは、右から左へ結合する順序で(++p)に等価であり、++p(++は前、先に後、後に後)を先に実行し、実行後、pは3番目の文字、すなわちlを指し、式(++p)はpが増加した値を返すので、最終結果はlとなる.
    同様に、pa++は(pa++)に等価であり、実行された結果は、式値が6(この結果、変数pa自体ではなく(pa++)式が作用し、pa値が1増加し、未知のメモリを指すことを証明することができる.++paを実行すると,paがこのとき指す位置が未知であるため,その中の内容を自己加算した結果も未知であるため,最終的な結果は1つのゴミ数である.
    strlen関数とsizeofオペレータ
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    
    strlen(char*)             (  string.h      ,string      ),               '\0',             ,        ,   aa        ,    '\0'  。
    char aa[10];cout<
  • 小知識
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    continue         ,             ,        。
     switch   continue   。
    continue     for、while、do-while     ,   if        ,       。
    
          if  。
    
            :
    int main()
    {
        int i;
        for(i = 0; i < 10; i ++)
        {
            if(i%2==0) continue;//  i      continue;
            printf("%d,", i);//  i 
        }
    }
    

    しんしんへんかん
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    
    1.itoa()  (     10         2-36     )
          :char*itoa(int value,char*string,int radix);
      :itoa(num, str, 2); num   int  ,     10   ,str     ,         。
    
        #include 
        #include 
        int main()  
        {  
            int num = 10;  
            char str[100];  
            _itoa(num, str, 2);  //c++    _itoa, itoa  ,
            printf("%s
    ", str); return 0; } ----------------- 2.strtol() :long int strtol(const char *nptr, char **endptr, int base) base , endptr,nptr , : #include int main() { char buffer[20]=" 10549stend# #12"; char *stop; int ans=strtol(buffer, &stop, 8); // 1054 , printf("%d
    ",ans); printf("%s
    ", stop); return 0; } : 556 9stend# #12 --------------- 3. string intToA(int n,int radix) //n ,radix { string ans=""; do{ int t=n%radix; if(t>=0&&t<=9) ans+=t+'0'; else ans+=t-10+'a'; n/=radix; }while(n!=0); // do{}while() 0 reverse(ans.begin(),ans.end());// , ; ( 2 , 0 , , ) return ans; }
  • 乱数の使用
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
    #include
    using namespace std;
    
    int main(){
    	time_t t1,t2;
    	t1=time(NULL);
    	t2=time(0); 
    	cout<