C++ 字符串也能做下标?之 [ ] 运算符重载

[ ] 运算符重载,在一些特定的场合会有奇效;
具体方法和关系运算符赋值运算符等都是一样的;

它有两种形式:
字符串下标法数字下标法

我将举一个例子详细说明 [ ] 运算符重载会在哪些场合使用,后续又该如何优化…

需求:
1.定义一个Boy类,里面有private成员name, age, salary 和 coefficient;
2.根据Boy类定义出两个对象boy1 和 boy2 并赋值:
Boy boy1(“张三”, 21, 20000, 10);
Boy boy2(“李四”, 22, 22000, 8);
3. 利用字符串下标法 和 数字下标法将他们打印出来
4. 将代码进行优化


1.定义一个Boy类,里面有private成员name, age, salary 和 coefficient;

Boy.h

#pragma once
#include <string>

class Boy {
public:
	Boy(const char *name = "无名", int age = 0, int salary = 0, int coefficient = 0);
	~Boy();

	char *getName() const;
	
	// 下标运算符重载
	const int operator[](std::string index) const;	// 字符串下标
	int operator[](int index) const;			// 数字下标

private:
	char *name;
	int age;
	int salary;
	int coefficient;	// 系数
};

Boy.cpp

#include "Boy.h"

Boy::Boy(const char *name, int age, int salary, int coefficient) {
	this->name = new char[strlen(name)+1];
	strcpy_s(this->name, strlen(name)+1, name);

	this->age = age;
	this->salary = salary;
	this->coefficient = coefficient;
}

Boy::~Boy() {
	if (name) {
		delete[] name;
	}
}

char* Boy::getName() const {
	return name;
}

// 字符串下标
const int Boy::operator[](std::string index) const {
	if (index == "age") {
		return age;
	} else if (index == "salary") {
		return salary;
	} else if (index == "coefficient") {
		return coefficient;
	} else {
		return -1;
	}
}

// 数字下标
int Boy::operator[](int index) const {
	if (index == 0) {
		return age; 
	} else if (index == 1) {
		return salary;
	} else if (index == 2) {
		return coefficient;
	} else {
		return -1;
	}
}

2.根据Boy类定义出两个对象boy1 和 boy2 并赋值:
Boy boy1(“张三”, 21, 20000, 10);
Boy boy2(“李四”, 22, 22000, 8);

#include <iostream>
#include "Boy.h"

using namespace std;

int main(void) {
	Boy boy1("张三", 21, 20000, 10);
	Boy boy2("李四", 22, 22000, 8);


	******3. 利用字符串下标法 和 数字下标法将他们打印出来******
	/********************************************************/
	cout << "字符串下标:" << endl;
	cout << "name = " << boy1.getName() << endl;
	cout << "age = " << boy1["age"] << endl;
	cout << "salary = " << boy1["salary"] << endl;
	cout << "coefficient = " << boy1["coefficient"] << endl;

	cout << endl << "数字下标:" << endl;
	cout << "name = " << boy2.getName() << endl;
	cout << "age = " << boy2[0] << endl;
	cout << "salary = " << boy2[1] << endl;
	cout << "coefficient = " << boy2[2] << endl;

	system("pause");
	return 0;
}

运行截图:

在这里插入图片描述

4. 将代码进行优化

代码中还有那些地方可以进行优化的呢?

其实可以优化的地方就在下标当中:

1.字符串下标中,下标是常量字符串,如果在写代码时写错了,编译器并不会报错,也可以正确运算,只是会找不到对应的数据,最总返回-1;而且如果上百万行代码,使用字符串常量下标的话,如果有一处需要修改,那么需要把这上百万代码都该掉,这是一个很大的工作量,所以,优化应使用宏定义最佳。

2.数字下标中,下标是数字,不可读,看着数字0, 1, 2… 看代码的人并不知道这些数字代表的是什么意思,所以,优化应使用枚举类型最佳。

一、字符串下标优化

在Boy.h中定义宏定义:

#define AGE_KEY			"age"
#define SALARY_KEY		"salary"
#define COEFFICIENT_KEY "coefficient"

二、数字下标优化

在Boy.h中定义枚举类型

typedef enum {
	AGE,
	SALARY,
	COEFFICIENT
};

Boy.h 代码优化

#pragma once
#include <string>

#define AGE_KEY			"age"
#define SALARY_KEY		"salary"
#define COEFFICIENT_KEY "coefficient"

typedef enum {
	AGE,
	SALARY,
	COEFFICIENT
};

class Boy {
public:
	Boy(const char *name = "无名", int age = 0, int salary = 0, int coefficient = 0);
	~Boy();

	char *getName() const;

	// 下标运算符重载
	const int operator[](std::string index) const;	// 字符串下标
	int operator[](int index) const;			// 数字下标

private:
	char *name;
	int age;
	int salary;
	int coefficient;	// 系数
};

Boy.cpp 代码优化

#include "Boy.h"

Boy::Boy(const char *name, int age, int salary, int coefficient) {
	this->name = new char[strlen(name)+1];
	strcpy_s(this->name, strlen(name)+1, name);

	this->age = age;
	this->salary = salary;
	this->coefficient = coefficient;
}

Boy::~Boy() {
	if (name) {
		delete[] name;
	}
}

char* Boy::getName() const {
	return name;
}

const int Boy::operator[](std::string index) const {
	if (index == AGE_KEY) {
		return age;
	} else if (index == SALARY_KEY) {
		return salary;
	} else if (index == COEFFICIENT_KEY) {
		return coefficient;
	} else {
		return -1;
	}
}

int Boy::operator[](int index) const {
	if (index == AGE) {
		return age; 
	} else if (index == SALARY) {
		return salary;
	} else if (index == COEFFICIENT) {
		return coefficient;
	} else {
		return -1;
	}
}

main函数代码优化

#include <iostream>
#include "Boy.h"

using namespace std;

int main(void) {
	Boy boy1("张三", 21, 20000, 10);
	Boy boy2("李四", 22, 22000, 8);

	/*cout << "字符串下标:" << endl;
	cout << "name = " << boy1.getName() << endl;
	cout << "age = " << boy1["age"] << endl;
	cout << "salary = " << boy1["salary"] << endl;
	cout << "coefficient = " << boy1["coefficient"] << endl;

	cout << endl << "数字下标:" << endl;
	cout << "name = " << boy2.getName() << endl;
	cout << "age = " << boy2[0] << endl;
	cout << "salary = " << boy2[1] << endl;
	cout << "coefficient = " << boy2[2] << endl;*/

	cout << "字符串下标:" << endl;
	cout << "name = " << boy1.getName() << endl;
	cout << "age = " << boy1[AGE_KEY] << endl;
	cout << "salary = " << boy1[SALARY_KEY] << endl;
	cout << "coefficient = " << boy1[COEFFICIENT_KEY] << endl;

	cout << endl << "数字下标:" << endl;
	cout << "name = " << boy2.getName() << endl;
	cout << "age = " << boy2[AGE] << endl;
	cout << "salary = " << boy2[SALARY] << endl;
	cout << "coefficient = " << boy2[COEFFICIENT] << endl;

	system("pause");
	return 0;
}

运行截图:

在这里插入图片描述

完美运算, 而且优化也完美!

使用 [ ] 运算符重载的优点: 可以使代码更可读。


总结:

1.在写代码时,应尽量避免常量,使用宏定义代替它;
2.写代码能优化尽量优化,这时初级程序员和高级程序员的分水岭;
3.返回值类型不同不能重载:
在代码中,我并没有把name也写入[ ] 重载运算函数中,也并没有独立写一个,
const int operator[ ] (std::string index) const;
const std::string operator[ ] (std::string index) const;
这样写会报错的,编译器不允许这样写,这是一个的注意的地方。

发布了39 篇原创文章 · 获赞 17 · 访问量 3833

猜你喜欢

转载自blog.csdn.net/cpp_learner/article/details/104231646