实现一个String类
String类原型:
class String
{
private:
char * str;
int len;
static int num_strings;
static const int CINLIM = 80;
};
需要实现的功能:
- 实现char*和String类的构造,提供无参构造
- 实现=,[],>,<,==,<<,>>,运算符重载
- 功能函数
函数名 | 功能 |
---|
append | 将字符添加到字符串的末尾。 |
assign | 将字符添加到字符串的末尾。 |
at | 返回对字符串中指定位置的元素的引用。 |
begin | 返回一个迭代器,寻址字符串中的第一个元素。 |
c_str | 将字符串的内容转换为C样式,以null终止的字符串。 |
capacity | 返回在不增加字符串的内存分配的情况下可以存储在字符串中的最大元素数。 |
cbegin | 返回一个指向字符串中第一个元素的const迭代器。 |
cend | 返回一个常量迭代器,该迭代器可寻址字符串中最后一个元素之后的位置。 |
clear | 擦除字符串的所有元素。 |
compare | 将字符串与指定字符串进行比较,以确定两个字符串是否相等,或者在字典上小于另一个。 |
copy | 从源字符串中的索引位置最多复制指定数量的字符到目标字符数组。 |
crbegin | 返回一个常量迭代器,该迭代器处理反向字符串中的第一个元素。 |
crend | 返回一个常量迭代器,该迭代器寻址反向字符串中最后一个元素之后的位置。 |
data | 将字符串的内容转换为字符数组。 |
empty | 测试字符串是否包含字符。 |
end | 返回一个迭代器,该迭代器寻址字符串中最后一个元素之后的位置。 |
erase | 从指定位置删除字符串中的一个元素或元素范围。 |
find | 在向前方向的字符串中搜索与指定字符序列匹配的子字符串的首次出现。 |
front | 返回对字符串中第一个元素的引用。 |
insert | 在字符串中的指定位置插入一个或多个元素或一系列元素。 |
length | 返回字符串中的当前元素数。 |
max_size | 返回字符串可以包含的最大字符数。 |
pop_back | 擦除字符串的最后一个元素。 |
push_back | 在字符串末尾添加一个元素。 |
rbegin | 返回一个迭代器,该迭代器返回反向字符串中的第一个元素。 |
rend | 返回一个迭代器,该迭代器指向恰好超出反向字符串中最后一个元素的位置。 |
replace | 将指定位置的字符串中的元素替换为指定字符或从其他范围或字符串或C字符串复制的字符。 |
reserve | 将字符串的容量设置为至少等于指定数字的数字。 |
resize | 指定字符串的新大小,并根据需要添加或删除元素。 |
rfind | 向后搜索字符串,查找与指定字符序列匹配的子字符串的首次出现。 |
size | 返回字符串中的当前元素数。 |
substr | 从指定位置开始的字符串中复制最多包含一些字符的子字符串。 |
swap | 交换两个字符串的内容。 |
String类示例:
#ifndef STRING_H_
#define STRING_H_
#include <iostream>
using std::ostream;
using std::istream;
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; }
int size() const { return len; }
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
String & operator+(const String &st);
String & operator+(const char *s);
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 bool operator==(const String &st, const char *st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator>>(istream & is, String & st);
static int HowMany();
public:
void append(const String &st);
void append(const char*s);
void push_back(const char ch);
void swap(char* s);
void swap(String& st);
int find(const char*s, size_t index);
int find(const String&st, size_t index);
};
#endif
#include <cstring>
#include "string.h"
using std::cin;
using std::cout;
int String::num_strings = 0;
int String::HowMany()
{
return num_strings;
}
void String::append(const String & st)
{
strcat(this->str, st.str);
this->len = strlen(this->str);
return;
}
void String::append(const char * s)
{
strcat(this->str, s);
this->len = strlen(this->str);
return;
}
void String::push_back(const char ch)
{
strcat(this->str, &ch);
this->len = strlen(this->str);
return;
}
void String::swap(char * s)
{
char temp[CINLIM];
strcpy(temp, this->str);
strcpy(this->str,s);
strcpy(s, temp);
this->len = strlen(this->str);
return;
}
void String::swap(String & st)
{
char temp[CINLIM];
strcpy(temp, this->str);
strcpy(this->str, st.str);
strcpy(st.str, temp);
this->len = strlen(this->str);
st.len = strlen(st.str);
return;
}
int String::find(const char * s, size_t index)
{
int length = strlen(s);
for (int this_index = index; this_index < this->len; this_index++)
{
if (this->str[this_index] == s[0])
{
for (int s_index = 0; s_index < length; s_index++)
{
if (this->str[this_index + s_index] != s[s_index])
break;
if (s_index == length - 1)
return this_index;
}
}
}
return -1;
}
int String::find(const String & st, size_t index)
{
int length = strlen(st.str);
for (int this_index = index; this_index < this->len; this_index++)
{
if (this->str[this_index] == st.str[0])
{
for (int s_index = 0; s_index < length; s_index++)
{
if (this->str[this_index + s_index] != st.str[s_index])
break;
if (s_index == length - 1)
return this_index;
}
}
}
return -1;
}
String::String(const char * s)
{
len = std::strlen(s);
str = new char[len + 1];
std::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];
std::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];
std::strcpy(str, st.str);
return *this;
}
String & String::operator=(const char * s)
{
delete [] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
}
char & String::operator[](int i)
{
return str[i];
}
const char & String::operator[](int i) const
{
return str[i];
}
String & String::operator+(const String & st)
{
strcat(this->str, st.str);
return *this;
}
String & String::operator+(const char * s)
{
strcat(this->str, s);
return *this;
}
bool operator<(const String &st1, const String &st2)
{
return (std::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 (std::strcmp(st1.str, st2.str) == 0);
}
bool operator==(const String & st, const char * st2)
{
return (strcmp(st.str, st2) == 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;
}
测试代码:
#include<iostream>
#include"string.h"
int main()
{
String s1 = "123";
String s2("abc");
String* s3 = new String("xyz");
s1 = s2;
if (s1 == s2)
{
cout << "s1 = s2" << endl;
}
cout << "===========拼接字符串============" << endl;
s1 = s1 + "456";
cout << s1 << endl;
s1.append("789");
cout << s1 << endl;
s1.push_back('!');
cout << s1 << endl;
cout << s1 << " " << s2 << endl;
cout << "===========交换字符串============" << endl;
s1.swap(s2);
cout << s1 << " " << s2 << endl;
cout << s2.size() << endl;
cout << "===========寻找字符串============" << endl << s2.find("456", 0) << endl;
cin.get();
return 0;
}
测试结果:
s1 = s2
===========拼接字符串============
abc456
abc456789
abc456789!
abc456789! abc
===========交换字符串============
abc abc456789!
10
===========寻找字符串============
3