• 数组操作符重载


    C++里面也可使用数组运算操作符:

    例如:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     string s = "a1b2c3d4e";
     9     int n = 0;
    10         
    11     for(int i = 0; i<s.length(); i++)
    12     {
    13         if( isdigit(s[i]) )
    14         {
    15             n++;
    16         }
    17     }
    18     
    19     cout << n << endl;
    20     
    21     return 0;
    22 }

    但是不是我们定义 了一个类后,就可以使用数组访问操作符了呢?

    被忽略的事实:

      -数组访问符是C/C++的内置操作符;

      -数组访问符的原生意义:数组访问指针运算

    例如:

      a[n] <->*(a+n) <->*(n+a) <-> n[a]


    指针与数组的复习.cpp:

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     int a[5] = {0};
     9     
    10     for(int i=0; i<5; i++)
    11     {
    12         a[i] = i;
    13     }
    14     
    15     for(int i=0; i<5; i++)
    16     {
    17         cout << *(a + i) << endl;    // cout << a[i] << endl;
    18     }
    19     
    20     cout << endl;
    21     
    22     for(int i=0; i<5; i++)
    23     {
    24         i[a] = i + 10;    // a[i] = i + 10;
    25     }
    26     
    27     for(int i=0; i<5; i++)
    28     {
    29         cout << *(i + a) << endl;  // cout << a[i] << endl;
    30     }
    31     
    32     return 0;
    33 }

    重载数组访问操作符:

    -数组访问操作符(【】)

      -只能通过类的成员函数重载

      -重载函数能且仅能使用一个参数

      -可以定义不同参数的多个重载函数。

    实例二(非常有意义):

     1 #include <iostream>
     2 #include <string>
     3 
     4 using namespace std;
     5 
     6 class Test
     7 {
     8     int a[5];
     9 public:
    10     int& operator [] (int i)
    11     {
    12         return a[i];
    13     }
    14     
    15     int& operator [] (const string& s)
    16     {
    17         if( s == "1st" )
    18         {
    19             return a[0];
    20         }
    21         else if( s == "2nd" )
    22         {
    23             return a[1];
    24         }
    25         else if( s == "3rd" )
    26         {
    27             return a[2];
    28         }
    29         else if( s == "4th" )
    30         {
    31             return a[3];
    32         }
    33         else if( s == "5th" )
    34         {
    35             return a[4];
    36         }
    37         
    38         return a[0];
    39     }
    40     
    41     int length()
    42     {
    43         return 5;
    44     }
    45 };
    46 
    47 int main()
    48 {
    49     Test t;
    50     
    51     for(int i=0; i<t.length(); i++)
    52     {
    53         t[i] = i;
    54     }
    55     
    56     for(int i=0; i<t.length(); i++)
    57     {
    58         cout << t[i] << endl;
    59     }
    60     
    61     cout << t["5th"] << endl;
    62     cout << t["4th"] << endl;
    63     cout << t["3rd"] << endl;
    64     cout << t["2nd"] << endl;
    65     cout << t["1st"] << endl;
    66     
    67     return 0;
    68 }

    提示:后面我们将学习智能指针,这是非常重要的一个概念,将来越来越多的会抛弃指针。

    小结:

      string类最大程度的兼容了C字符串的用法。

      数组访问符的重载能够使得对象模拟数组的行为

      只能通过类的成员函数重载数组访问符

      重载函数能且仅能使用一个参数。

  • 相关阅读:
    Fiddler响应post的请求 request body里面填写什么?
    intellij idea 插件 ideaVim 用法
    Ubuntu 配置有线网 IP
    Git TortoiseGit SSH设置
    QT+QT creator+OpenCV图像灰度化
    用的最多的Android Studio 快捷键
    做高通平台安卓驱动感言
    《高可用MySQL》2 – 单机版MySQL主从配置
    JAVA学习第四十七课 — IO流(一):文件的读写
    hive 配置文件以及join中null值的处理
  • 原文地址:https://www.cnblogs.com/lemaden/p/10121594.html
Copyright © 2020-2023  润新知