34 - 数组操作符的重载

本文探讨了如何在C++中通过string类实现类似C字符串的灵活访问,并介绍了如何重载数组访问操作符,通过实例演示了如何用C方式处理string对象及如何支持数组下标访问。涉及内容包括兼容性实验、指针与数组的复习,以及自定义类的重载操作符实现。
摘要由CSDN通过智能技术生成

---- 整理自狄泰软件唐佐林老师课程

1. 问题一

  • string 类对象具备 C 方式字符串的灵活性吗?还能直接访问单个字符吗?

1.1 字符串类的兼容性

  • string 类最大限度的考虑了 C 字符串的兼容性
  • 可以按照使用 C 字符串的方式使用 string 对象
    在这里插入图片描述

1.2 编程实验:用 C 方式使用 string 类

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s = "a1b2c3d4e5fg";
    int n = 0;
	
	for (int i = 0; i < s.length(); i++) {
		if (isdigit(s[i])) {
			n++;
		}
	}
	
	cout << n << endl;
    
    return 0;
}

在这里插入图片描述

2. 问题二

  • 类的对象怎么支持数组的下标访问?
#include <iostream>
#include <string>

using namespace std;

class Test
{
	int a[5];
public:
	
};

int main()
{
    Test t;
	
	cout << t[1] << endl;
    
    return 0;
}

在这里插入图片描述

2.1 重载数组访问操作符

  • 数组访问符是 C / C++ 中的内置操作符
  • 数组访问符的原生意义是数组访问和指针运算

在这里插入图片描述

2.2 实例分析:指针和数组的复习

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int a[5] = {0};
    
    for(int i=0; i<5; i++) {
        a[i] = i;
    }
    
    for(int i=0; i<5; i++) {
        cout << *(a + i) << endl;    // cout << a[i] << endl;
    }
    
    cout << endl;
    
    for(int i=0; i<5; i++) {
        i[a] = i + 10;    // a[i] = i + 10;
    }
    
    for(int i=0; i<5; i++) {
        cout << *(i + a) << endl;  // cout << a[i] << endl;
    }
    
    return 0;
}

在这里插入图片描述

2.3 数组访问操作符 [ ]

  • 只能通过类的成员函数重载
  • 重载函数能且只能使用一个参数
  • 可以定义不同参数的多个重载函数

2.4 编程实验:重载数组访问操作符

#include <iostream>
#include <string>

using namespace std;

class Test
{
	int a[5];
public:
	int operator[](int i) {
		return a[i]; // 函数调用的返回值不能作为左值
	}
	int length() {
		return 5;
	}
};

int main()
{
    Test t;
	
	for (int i = 0; i < t.length(); i++) {
		// t[i] = i;
		t.operator[](i) = i;
	}
	
	for (int i = 0; i < t.length(); i++) {
		cout << t[i] << endl;
	}
    
    return 0;
}

在这里插入图片描述

  • 注解:事实上在执行 return 语句时系统是在内部自动创建了一个临时变量,然后将 return 要返回的那个值赋给这个临时变量。所以当被调函数运行结束后 return 后面的返回值就被释放掉了,最后是通过这个临时变量将值返回给主调函数的。而且定义函数时指定的返回值类型实际上指定的就是这个临时变量的类型。这些都是系统自动完成的,了解即可。
  • 用引用解决上述函数调用的返回值不能作为左值使用的问题:
#include <iostream>
#include <string>

using namespace std;

class Test
{
	int a[5];
public:
	int& operator[](int i) {
		return a[i]; // 返回引用
	}
	int length() {
		return 5;
	}
};

int main()
{
    Test t;
	
	for (int i = 0; i < t.length(); i++) {
		t[i] = i;
		// t.operator[](i) = i;
	}
	
	for (int i = 0; i < t.length(); i++) {
		cout << t[i] << endl;
	}
    
    return 0;
}

在这里插入图片描述

  • 注解:在一个类中,
    • 函数返回 值类型:在执行 =(赋值)时,在内存中会出现 临时变量,由临时变量执行 =(赋值操作)。
    • 函数返回 引用类型:在执行 =(赋值)时,不会出现临时变量情况,只对数据进行了拷贝。

在这里插入图片描述

#include <iostream>
#include <string>

using namespace std;

class Test
{
    int a[5];
public:
    int& operator[](int i) {
        return a[i];
    }
	int& operator[](const string& s) {
        if( s == "1st" ) {
            return a[0];
        } else if( s == "2nd" ) {
            return a[1];
        } else if( s == "3rd" ) {
            return a[2];
        } else if( s == "4th" ) {
            return a[3];
        } else if( s == "5th" ) {
            return a[4];
        }
        
        return a[0];
    }
    int length() {
        return 5;
    }
};

int main()
{
    Test t;
    
    for(int i=0; i<t.length(); i++) {
        t[i] = i;
    }
    
    for(int i=0; i<t.length(); i++) {
        cout << t[i] << endl;
    }
    
    cout << t["5th"] << endl;
    cout << t["4th"] << endl;
    cout << t["3rd"] << endl;
    cout << t["2nd"] << endl;
    cout << t["1st"] << endl;
    
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

uuxiang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值