改进后的新String类

改进后的新String类

对StringBad类进行修订,将它重命名为String了。首先,添加了复制构造函数和赋值运算符,使类能够正确管理类对象使用的内存。其次,由于知道对象何时被创建和释放,因此可以让类构造函数和析构函数保持沉默,不再每次被调用时都显示消息。另外,也不再监视构造函数的工作情况,因此可以简化默认构造函数,使之创建一个空字符串,而不是C++。

接下来,可以在类中添加一些新功能。String类应该包含标准字符串函数库cstring的所有功能,才会比较有用,但这里只添加足以说明其工作原理的功能:

int length() const {
    
     return len; }
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend operator>>(istream &is, String &st);
char & operator[](int i);
const char & operator[](int i) const;
static int HowMany();

第一个新方法返回被存储的字符串的长度。接下来的3个友元函数能够对字符串进行比较。Operator>>()函数提供了简单的输入功能;两个operator[]()函数提供了以数组表示法访问字符串中各个字符的功能。静态类方法Howmany()将补充静态类数据成员num_string。

修订后的默认构造函数

注意新的默认构造函数,它与下面类似:

String::String()
{
    
    
	len = 0;
	str = new char[1];
	str[0] = '\0'; //默认字符串
}

为什么代码为:

str = new char[1];

而不是:

str = new char;

上面两种方式分配的内存量相同,区别在于前者与类析构函数兼容,而后者不兼容。

析构函数中包含:

delete[] str;

delete[]与使用new[]初始化的指针和空指针都兼容,对于下述代码:

str = new char[1];
str[0] = '\0';

可修改为:

str = 0;

对于以其他方式初始化的指针,使用delete[]时,结果将是不确定的:

char worda[15] = "bad idea";
char *p1 = words;
char *p2 = new char;
char *p3;
delete[] p1;
delete[] p2;
delete[] p3;

C++11空指针

在C++98中,字面值0有两个含义:表示数字值零;表示空指针。C++11引入新关键字nullptr,用于表示空指针。

str = nullptr;

比较成员函数

在String类中,执行比较操作的方法有3个。如果按字母顺序,第一个字符串在第二个字符串之前,则operator<()函数返回true。要实现字符串比较函数,最简单的方法是使用标准的strcmp()函数,如果依照字母顺序,第一个参数位于第二个参数之前,则该函数返回一个负值;如果两个字符串相同,则返回0;如果第一参数位于第二个参数之后,则返回一个正值。因此,可以这样使用strcmp():

bool operator<(const String & st1, const String & st2)
{
    
    
	if (strcmp(st1.str, st2.str) < 0)
		return true;
	else
		return false;
}

因为内置的>运算符返回的是一个布尔值,所以可以将代码进一步简化为:

bool operator<(const String & st1, const String & st2)
{
    
    
	return (strcmp(st1.str, st2.str) < 0);
}

同样,可以按照下面的方式编写另外两个比较函数:

bool operator>(const String & st1, const String & st2)
{
    
    
	return st2 < st1;
}

bool operator==(const String & st1, const String & st2)
{
    
    
	return (strcmp(st1.str, st2.str) == 0);
}

第一个定义利用了<运算符表示>运算符,对于内联函数,这是一个很好的选择。

比较函数作为友元,有助于将String对象与常规的C字符串进行比较。假设answer是String对象:

if("love" == answer)

将被转换为:

if(operator == ("love", answer))

然后,编译器将使用某个构造函数将代码转换为:

if(operator == (String("love"), answer))

这与原型是相匹配的。

使用中括号表示访问字符

对于C风格字符串来说,使用中括号来访问其中的字符:

char city = "Amsterdam";
cout << city[0] << endl; //输出字母A

在C++中,两个中括号组成一个运算符——中括号运算符,使用方法operator来重载该运算符。二元C++运算符(带两个操作数)位于两个操作数之间,例如2+5。但对于中括号运算符,一个操作数位于第一个中括号的前面,另一个操作数位于两个中括号之间。因此,在表达式city[0]中,city是第一个操作数,[]是运算符,0是第二个操作数。

假设opera是一个String对象:

String opera("The Magic Flute");

则对于表达式opera[4],C++将查找名称和特征标与此相同的方法:

String::operator[](int i)

如果找到匹配的原型,编译器将使用下面的函数调用来替代表达式opera[4]:

opera.operator[](4)

opera对象调用该方法,数组下标4成为该函数的参数。

下面是该方法的简单实现:

char & String::operator[](int i)
{
    
    
	return str[i];
}

有了上述定义后,语句:

cout << opera[4];

被转换为:

cout << opera.operator[4];

返回值是opera.str[4](字符M)。由此,公有方法可以访问私有数据。

将返回类型声明为char&,便可以给特定元素赋值。

String means("might");
means[0] = 'r';

第二条语句将被转换为一个重载运算符函数调用:

means.operator[][0] = 'r';

这里将r赋给方法的返回值,而函数返回的是指means.str[0]的引用,因此上述代码等同于下面的代码:

means.str[0] = 'r';

代码的最后一行访问的是私有数据,但由于operator是类的一个方法,因此能够修改数组的内容。最终的结果是"might"被改为"right"。

假设有下面的常量对象:

const String answer("futile");

如果只有上述operator定义,则下面的代码将出错。

cout << answer[1];

原因是answer是常量,而上述方法无法确保不修改数据(实际上,有时候该方法的工作就是修改数据,因此无法确保不修改数据)。

但在重载时,C++将区分常量和非常量函数的特征标,因此可以提供另一个仅供const String对象使用的operator版本:

const char & String::operator[](int i) const
{
    
    
	return str[i];
}

有了上述定义后,就可以读 / 写常规String对象了;而对于const String对象,则只能读取其数据:

String text("Once upon a time"); 
const String answer("futile"); 
cout << text[1]; //可行,调用了没有const的operator[]()
cout << answer[1]; //可行,调用了const的operator[]()
cin >> text[1]; //可行,调用了没有const的operator[]()
cin >> answer[1]; //错误

静态类成员函数

将成员函数声明为静态的(函数声明必须包含关键字static,但如果函数定义是独立的,则其中不能包含关键字static),这样做有两个重要的后果。

首先,不能通过对象调用静态成员函数;实际上,静态成员函数甚至不能使用this指针。如果静态成员函数是在公有部分声明的,则可以使用类名和作用域解析运算符来调用它。

例如,可以给String类添加一个名为Howmany()的静态成员函数,方法是在类声明中添加:

static int HowMany();

调用它的方式:

int count = String::HowMany();

其次,由于静态成员函数不与特定的对象相关联,因此只能使用静态数据成员。例如,静态方法HowMany()可以访问静态成员num_string,但不能访问str和len。

同样,使用静态成员函数设置类级(classwide)标记,以控制某些类接口的行为。例如,类级标记可以控制显示类内容的方法所使用的格式。

进一步重载赋值运算符

假设要将常规字符串复制到String对象中。例如,使用getline()读取了一个字符串,并要将这个字符串放置到String对象中:

String name;
char temp[40];
cin.getline(temp, 40);
name = temp; //利用构造函数进行类型转换

但这不是一个理想的解决方案。最后一条语句的工作:
1)程序使用构造函数String(const char*)来创建一个临时String对象,其中包含temp中的字符串副本。只有一个参数的构造函数被用作转换函数。
2)使用String & String::operator=(const String&)函数将临时对象中的信息复制到name对象中。
3)程序调用析构函数~String()删除临时对象。
为了提高处理效率,最简单的方法是重载赋值运算符,使之能够直接使用常规字符串,这样就不用创建和删除临时对象了。

String & String::operator=(const char *s)
{
    
    
	delete[] str;
	len = strlen(s);
	str = new char[len + 1];
	strcpy(str, s);
	return *this;
}

一般来说,必须释放str指向的内存,并为新字符串分片足够的内存。

string1.h

#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
using namespace std;

class String
{
    
    
private:
	char *str; //指向字符的指针
	int len; //字符的长度
	static int num_strings; //对象个数
	static const int CINLIM = 80; //输入的限制

public:
	String(const char *s); //构造函数
	String(); //默认构造函数
	String(const String &); //拷贝构造函数
	~String(); //析构函数
	int length() const {
    
     return len; }
	
	//重载运算符成员函数
	String & operator=(const String &); 
	String & operator=(const char *);
	char & operator[](int i);
	const char & operator[](int i) const;

	//重载运算符友元函数
	friend bool operator<(const String &st, const String &st2);
	friend bool operator>(const String &st1, const String &st2);
	friend bool operator==(const String &st, const String &st2);
	friend ostream & operator<<(ostream &os, const String &st);
	friend istream & operator>>(istream &is, String &st);

	//静态成员函数
	static int HowMany();
};
#endif

string1.cpp

#include "string1.h"
#include <cstring>

//静态类成员初始化
int String::num_strings = 0;

//静态方法
int String::HowMany()
{
    
    
	return num_strings;
}

//类方法
String::String(const char * s)
{
    
    
	len = strlen(s);
	str = new char[len + 1];
	strcpy(str, s);
	num_strings++;
}

String::String()
{
    
    
	len = 4;
	str = new char[1];
	str[0] = '\0';
	num_strings;
}

String::String(const String &st)
{
    
    
	num_strings++;
	len = st.len;
	str = new char[len + 1];
	strcpy(str, st.str);
}

String::~String()
{
    
    
	--num_strings;
	delete[] str;
}
/*重载运算符成员函数*/
//字符串赋值给字符串
String & String::operator=(const String &st)
{
    
    
	if (this == &st)
		return *this;
	delete[] str;
	len = st.len;
	str = new char[len + 1];
	strcpy(str, st.str);
	return *this;
}

//char型字符串赋值给String型字符串
String & String::operator=(const char *s)
{
    
    
	delete[] str;
	len = strlen(s);
	str = new char[len + 1];
	strcpy(str, s);
	return *this;
}

//读/写常规String对象的字符访问
char & String::operator[](int i)
{
    
    
	return str[i];
}

//只读的常量String对象的字符访问
const char & String::operator[](int i) const
{
    
    
	return str[i];
}

//重载运算符友元函数
bool operator<(const String & st1, const String & st2)
{
    
    
	return (strcmp(st1.str, st2.str) < 0);
}

bool operator>(const String & st1, const String & st2)
{
    
    
	return st2 < st1;
}

bool operator==(const String & st1, const String & st2)
{
    
    
	return (strcmp(st1.str, st2.str) == 0);
}

//简单的字符串输出
ostream & operator<<(ostream & os, const String & st)
{
    
    
	os << st.str;
	return os;
}

//简单快速的字符串输入
istream & operator>>(istream & is, String & st)
{
    
    
	char temp[String::CINLIM];
	is.get(temp, String::CINLIM);
	if (is)
		st = temp;
	while (is && is.get() != '\n')
		continue;
	return is;
}

重载>>运算符提供了一种将键盘输入行读入到String对象中的简单方法。它假定输入的字符数不多于String::CINLIM的字符数,并丢弃多余的字符。在if条件下,如果由于某种原因(如到达文件尾或get(char*, int)读取的是一个空行)导致输入失败,istream对象的值将置为false。

main.cpp

该程序允许输入几个字符串。程序首先提示用户输入,然后将用户输入的字符串存储到String对象中,并显示它们,最后指出哪个字符串最短、哪个字符串按字母顺序排在最前面。

#include <iostream>
#include "string1.h"
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
    
    
	String name;
	cout << "Hi, What's your name?\n>> ";
	cin >> name;

	cout << name << ", please enter up to " << ArSize
		<< " short sayings <empty line to quit>:\n";
	String sayings[ArSize]; //数组对象
	char temp[MaxLen]; //存储临时字符串
	int i;
	for (i = 0; i < ArSize; i++)
	{
    
    
		cout << i + 1 << ": ";
		cin.get(temp, MaxLen);
		while (cin && cin.get() != '\n')
			continue;
		if (!cin || temp[0] == '\0') //空行
			break; //i值没有增加
		else
			sayings[i] = temp; //赋值重载
	}
	int total = i; //读取的行数
	
	if (total > 0)
	{
    
    
		cout << "Here are your sayings:\n";
		for (i = 0; i < total; i++)
			cout << sayings[i][0] << ": " << sayings[i] << endl;
		int shortest = 0;
		int first = 0;
		for (i = 1; i < total; i++)
		{
    
    
			if (sayings[i].length() < sayings[shortest].length())
				shortest = i;
			if (sayings[i] < sayings[first])
				first = i;
		}
		cout << "Shortest saying:\n" << sayings[shortest] << endl;
		cout << "First alphabetically:\n" << sayings[first] << endl;
		cout << "This program used " << String::HowMany()
			<< " String objects. Bye. \n";
	}
	else
		cout << "No input! Bye. \n";
	return 0;
}

注意:较早的get(char*, int)在读取空行后,返回的值不为false。然而,对于这些版本来说,如果读取了一个空行,则字符串中第一个字符将是一个空字符。

if(!cin || temp[0] == '\')
   break;

如果实现遵循最新C++标准,则if语句中第一条件将检测到空行,第二个条件用于旧版本实现中检测空行。

程序要求用户输入至多10条谚语。每条谚语都被读到一个临时字符数组,然后被复制到String对象中。如果用户输入空行,break语句将终止输入循环。显示用户的输入后,程序使用成员函数length()和operator<()来确定最短的字符串以及按字母顺序排列在最前面的字符串。程序还使用下标运算符([])提取每条谚语的第一个字符,并将其放在该谚语的最前面。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36314864/article/details/119538312