模拟实现string
1. STL容器——string
1.1 标准库中的string类是什么?
- string是表示字符串的字符串类
- 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
- string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>
string;- 不能操作多字节或者变长字符的序列。
1.2 string有哪些成员函数?
1.3 string类常用的成员函数使用方法?
- 构造、拷贝构造
void Test()
{
string s1; // 构造空的string类对象s1
string s2("hello bit"); // 用C格式字符串构造string类对象s2
string s3(s2); // 拷贝构造s3
}
- string类对象容量操作
void Test1()
{
string s("hello world!!!");
cout << s.size() << endl; //返回字符串的有效长度
cout << s.length() << endl; //与size用法相同,更常用size
cout << s.capacity() << endl; //返回对象分配到的内存大小
s.resize(20,'a'); //将有效字符的个数改成20个,多出的空间用字符'a'填充
s.resize(3); //将有效字符的个数改成3个
s.reserve(50); //将字符串容量增加到50,如果参数大小小于字符串实际内存,则不会增容
s.clear(); //清空有效字符,分配到的内存不改变
s.empty(); //判断字符串是否为空串
}
- string类对象的访问和遍历操作
void Test2()
{
string s("hello world!");
cout << s[2] << endl; //利用[]运算符重载访问字符串中下标为2的字符
for(size_t i=0;i<s.size(),++i) //遍历字符串
{
cout << s[i] << endl;
}
string::iterator it = s.begin();//利用迭代器访问字符串,begin返回字符串起始位置的迭代器
while(it != s.end())
{
cout << *it;
it++;
}
for(auto c : s) //范围for访问字符串,本质还是利用了迭代器
{
cout << c;
}
}
- string类对象的修改操作
void Test3()
{
string s("hello world!");
s.push_back(' '); //给字符串尾插一个字符
s.append("abc"); //给字符串尾插一个字符串
s += 'd'; //给字符串尾插一个字符
s += "efg"; //给字符串尾插一个字符串
cout << s.c_str() << endl; //返回C格式的字符串
cout << s.find('o', 6) << endl; //从字符串6位置往后找字符‘o’,返回该字符在字符串中的位置
cout << s.rfind(' ', 8) << endl;//从字符串8位置往前找字符' ',返回该字符在字符串中的位置
s.erase(0 , 4); //删除字符串0-4位置的字符
string s1("123456789");
s1.swap(s); //交换两个字符串的值
}
- string类常用的非成员函数
void Test4()
{
string s("hello world!");
cout << s << endl; //输出运算符重载
cin >> s; //输入运算符重载
getline(cin,s); //可以将带空格的字符串输入进s中
string s1("hello");
cout << (s > s1) << (s <= s1) <<end; //大小比较运算符重载
}
2. 模拟实现string
2.1 模拟实现一些常用的接口,声明这些成员函数
string.h
namespace zhy{ //创建一个命名空间,防止与标准库中string类发生命名冲突
class string
{
public:
static const size_t npos; //size_t类型的最大值,做返回值时表示没有匹配,做字符串长度参数时表示到字符串末端
typedef char* iterator; //声明迭代器类型
string(const char* str = "");
string(const string& s); //拷贝构造
string& operator=(const string& s); //赋值运算符重载
~string(); //析构
//string类对象容量操作相关函数
size_t size();
size_t capacity();
bool empty();
void reserve(size_t capcity);
void resize(size_t n, char c = '\0');
void clean();
//string类对象访问和遍历操作相关函数
char operator[](size_t i);
iterator begin();
iterator end();
//string类对象修改操作相关函数
void push_back(char c);
void append(const char* str);
void operator+=(char c);
void operator+=(const char* s);
void operator+=(const string& str);
size_t find(char c, size_t pos = 0);
size_t find(const char* s, size_t pos = 0);
void insert(size_t pos, char c);
void insert(size_t pos, char* s);
void erase(size_t pos, size_t len = npos);
void swap(string& temp);
const char* c_str();
//比较大小运算符重载
bool operator<(const string& s);
bool operator<=(const string& s);
bool operator>(const string& s);
bool operator>=(const string& s);
bool operator==(const string& s);
bool operator!=(const string& s);
private:
char* _str; //指向存放字符空间的指针
size_t _size; //字符串中实际的字符数
size_t _capacity; //字符串分配到的内存大小
};
ostream& operator<<(ostream& out, zhy::string& s);
istream& operator>>(istream& in, zhy::string& s);
}
上述这些成员函数和非成员函数是string中比较常用的,下面来用代码简单实现他们
2.2 代码实现string常用的成员函数
- 构造函数
这里的构造函数必须我们自己实现,因为默认生成的构造函数只实现了简单的值拷贝(浅拷贝),在对象扩容或修改内容时会出现问题,下面看看浅拷贝与深拷贝的区别,以及深拷贝如何解决存在的问题:
const size_t zhy::string::npos = -1; //定义静态变量npos
zhy::string::string(const char* str)
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1]; //+1是为了在字符串结尾放'\0',标志字符串的结束
strcpy(_str, str);
}
- 拷贝构造函数
拷贝构造函数有两种写法:现代写法和传统写法。
传统写法:与上面构造函数的写法类似,直接开辟空间然后将字符串拷贝进开出的空间中;
现代写法:用传入的对象构造一个临时对象,直接将当前对象与临时对象交换内容即可。
zhy::string::string(const string& s)
:_str(nullptr) //如果不初始化成nullptr,_str有可能是随机值,交换后temp中是随机值,出作用域时temp会调用析构函数,析构一个随机地址会报错
{
string temp(s._str);
swap(temp);
}
- 赋值运算符重载
用现代写法对对象进行赋值
zhy::string& zhy::string:: operator=(const string& s) //传值会发生深拷贝,传引用可以提高效率
{
if (this != &s) //防止自己拷贝自己
{
string temp(s);
swap(temp);
}
return *this;
}
- 析构函数
zhy::string::~string()
{
if (_str) //判断_str是否为空
{
delete[] _str; //释放_str的空间
_str = nullptr; //将_str置为空
_capacity = _size = 0; //_capacity和_size置为0
}
}
- size
直接返回对象的成员变量_size,_size记录着字符串的有效字符个数
size_t zhy::string::size()
{
return _size;
}
- capacity
返回对象的成员变量_capacity,_capacity记录着字符串分配到的内存大小
size_t zhy::string::capacity()
{
return _capacity;
}
- empty
如果字符串中有效字符的个数为0,则代表该字符串是空串,返回true
bool zhy::string::empty()
{
if (_size == 0)
return true;
else
return false;
}
- resize
要注意每次修改后’\0’位置的变化和_size、_capacity的变化
void zhy::string::resize(size_t n, char c) //有效字符改成n个,多出的空间用字符c填充
{
if (n > _size)
{
if (n>_capacity)
{
reserve(n);
}
for (size_t i = _size; i < n; i++)
{
_str[i] = c;
}
}
_str[n] = '\0';
_size = n;
}
- reserve
void zhy::string::reserve(size_t capacity)
{
if (capacity > _capacity) //新开辟的空间比原空间大则增容
{
char* str = new char[capacity + 1]; //实际空间要比_capcity大一个,用来放'\0'
strcpy(str, _str); //将_str中的字符串拷贝到新开辟的空间
delete[] _str; //释放_str的空间
_str = str; //让_str指向新开辟的空间,实现增容
_capacity = capacity; //更新_capacity
}
}
- clean
让对象中的有效字符变为0个,只需要将_size改为0,’\0’放在第一个位置。
void zhy::string::clean()
{
_size = 0;
_str[_size] = '\0';
}
- [ ]运算符重载
char zhy::string:: operator[](size_t i)
{
assert(i < _size); //只能访问有效字符,i如果大于_size,就会访问内存越界
return _str[i];
}
- 迭代器begin和end
zhy::string::iterator zhy::string::begin()
{
return _str; //返回字符串第一个字符的位置
}
zhy::string::iterator zhy::string:: end()
{
return _str + _size; //返回最后一个字符的下一个位置,标志着结束
}
- push_back
void zhy::string::push_back(char c)
{
if (_size == _capacity) //空间满了就扩容
reserve(2 * _capacity+1); //如果_size=_capcity=0,开空间就会失败,所以不能用2*_capcity
_str[_size] = c; //尾插字符
_size++; //更新_size
_str[_size] = '\0'; //字符串结尾放'\0',标志字符结束
}
- append
void zhy::string::append(const char* str)
{
size_t len = strlen(str);
for (size_t i = 0; i < len; i++)
{
push_back(str[i]);
}
}
- +=运算符重载
这些运算符重载直接复用上面的函数就可以
void zhy::string:: operator+=(char c)
{
push_back(c);
}
void zhy::string::operator+=(const char* s)
{
append(s);
}
void zhy::string::operator+=(const string& str)
{
append(str._str);
}
- find
简单地遍历查找
size_t zhy::string::find(char c, size_t pos) //从pos位置向后找字符c第一次出现的位置
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == c)
return i;
}
return npos;
}
size_t zhy::string::find(const char* s, size_t pos) //找子串,若有返回子串的起始位置
{
const char* pch = strstr(_str + pos, s); //从pos位置找与字符串s相同的子串
if (pch != nullptr)
{
return pch - _str; //找到了就返回子串的初始位置
}
else
return npos;
}
- insert
插入字符和插入字符串方法相同,插入字符串需要移动字符的更多,下图是插入字符串的过程:
void zhy::string::insert(size_t pos, char c) //pos位置插入字符c
{
assert(pos <= _size);
if (_size == _capacity) //空间满了增容
{
reserve(2 * _capacity + 1);
}
size_t end = _size + 1; //从'\0'开始向后移动
while (end > pos) //将pos及其后面所有字符向后移动
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = c;
_size++;
}
void zhy::string::insert(size_t pos, char* s) //pos位置插入字符串s
{
assert(pos <= _size);
size_t len = strlen(s);
if (_size+len > _capacity) //插入前要保证有足够的空间
{
reserve(_size + len); //空间不够就要扩容
}
size_t end = _size + len; //插入后字符串的结束位置
while (end >= pos + len)
{
_str[end] = _str[end - len]; //从'\0'向后移动移动len个位置,为新插入的字符串空出位置
end--;
}
strncpy(_str + pos, s, len); //将要出插入的字符串直接插入对应的位置
_size += len;
}
- erase
void zhy::string::erase(size_t pos, size_t len) //从pos位置向后删除len个字符
{
assert(pos < _size);
if (len >= _size - pos || len == npos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t first = pos + len; //将pos+len后面的字符向前移动
while (first <= _size)
{
_str[first - len] = _str[first];
first++;
}
_size -= len;
}
}
- swap
void zhy::string::swap(string& temp)
{
_size = strlen(temp._str); //更新_size、_capacity
_capacity = _size;
std::swap(_str, temp._str); //交换_str,temp._str的空间,_str原有的空间temp出作用域时会释放掉
}
- c_str
const char* zhy::string::c_str()
{
return _str; //返回字符串的首地址
}
- 比较大小运算符重载
字符串比较大小的规则:
- 如果两字符串长度相等,比较两个字符串中第一个字符的ASCII码值,如果两值相同,则比较第二个字符,若相同,则比较第三个…如果全相同,则两字符串相等。如果比较过程中有两字符ASCII码值不同,哪个字符的ASCII值大,则该字符所在的字符串就大。
例:abcdef>abcdee ; abcd==abcd ; abc<bca- 如果两字符串长度不等,则它们不会相等,大小的比较按照上面的规则进行,如果在较短字符结束前比出大小,则结束。 如果比较到较短字符串末尾还没比出大小,则较短字符串小于长字符串。
例:abc>abaddd ; abc<abcaaa ; bca>abcjjj
注:标准库中关系运算符的重载是在类外实现的,复用比较方便直观,这里我将它们实现为类的成员函数,复用时需要用this指针调用
bool zhy::string::operator==(const string& s)
{
size_t i1 = 0, i2 = 0;
while (i1 < _size&&i2 < s._size) //比较两字符串中对应位置字符的ASCII码值
{
if (_str[i1] != s._str[i2]) //出现不同则两字符串不相等
return false;
else //没结束继续向后比较
{
i1++;
i2++;
}
}
if (i1 == _size&&i2 == s._size) //两字符串长度相同且对应位置的字符都相等
return true;
else //长度不同,不可能相等
return false;
}
bool zhy::string::operator<(const string& s)
{
size_t i1 = 0, i2 = 0;
while (i1 < _size&&i2 < s._size) //比较两字符串中对应位置字符的ASCII码值
{
if (_str[i1]>s._str[i2]) //_str中出现一个比s._str大的就代表_str大
return false;
else //没结束继续向后比较
{
i1++;
i2++;
}
}
if (i1 == _size&&i2 != s._size) //上面比较完都相同但是_str短,则_str小
return true;
else
return false;
}
bool zhy::string::operator<=(const string& s) //复用上面的函数,< || == 就是小于等于
{
if (this->operator<(s) || this->operator==(s))
return true;
else
return false;
}
bool zhy::string::operator>(const string& s) //复用上面的函数,不小于等于就是大于
{
if (this->operator<=(s))
return false;
else
return true;
}
bool zhy::string::operator>=(const string& s) //复用上面的函数,不小于就是大于等于
{
if (this->operator<(s))
return false;
else
return true;
}
bool zhy::string::operator!=(const string& s) //复用上面的函数,不等于
{
if (this->operator==(s))
return false;
else
return true;
}
- 输入输出运算符重载
ostream& zhy::operator<<(ostream& out, zhy::string& s)
{
for (size_t i = 0; i < s.size(); i++)
{
out << s[i];
}
return out; //返回out,可以连续输出
}
istream& zhy::operator>>(istream& in, zhy::string& s)
{
s.resize(0);
char c;
while (1)
{
//in >> c; //这样输入会导致死循环,空格不能被写进c中,循环无法退出
in.get(c); //用get可以读入空格、回车
if (c == ' ' || c == '\n')
{
break; //是空格或回车就结束输入
}
else
{
s.push_back(c); //将输入的字符尾插进字符串中
}
}
return in; //返回in,这样可以连续输入
}
2.3 完整代码
string.h
#pragma once
#include <assert.h>
#include <iostream>
using namespace std;
namespace zhy{
class string
{
public:
static const size_t npos;
typedef char* iterator;
string(const char* str = "");
string(const string& s);
string& operator=(const string& s);
~string();
size_t size();
size_t capacity();
bool empty();
void reserve(size_t capcity);
void resize(size_t n, char c = '\0');
void clean();
char operator[](size_t i);
iterator begin();
iterator end();
void push_back(char c);
void append(const char* str);
void operator+=(char c);
void operator+=(const char* s);
void operator+=(const string& str);
size_t find(char c, size_t pos = 0);
size_t find(const char* s, size_t pos = 0);
void insert(size_t pos, char c);
void insert(size_t pos, char* s);
void erase(size_t pos, size_t len = npos);
void swap(string& temp);
const char* c_str();
bool operator<(const string& s);
bool operator<=(const string& s);
bool operator>(const string& s);
bool operator>=(const string& s);
bool operator==(const string& s);
bool operator!=(const string& s);
private:
char* _str;
size_t _size;
size_t _capacity;
};
ostream& operator<<(ostream& out, zhy::string& s);
istream& operator>>(istream& in, zhy::string& s);
}
string.cpp
#include "string.h"
const size_t zhy::string::npos = -1;
zhy::string::string(const char* str)
{
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
zhy::string::string(const string& s)
:_str(nullptr) //如果不初始化成nullptr,_str有可能是随机值,交换后temp中是随机值,出作用域时temp会调用析构函数,析构一个随机地址会报错
{
string temp(s._str);
swap(temp);
}
zhy::string& zhy::string:: operator=(const string& s)
{
if (this != &s) //防止自己拷贝自己
{
string temp(s);
swap(temp);
}
return *this;
}
zhy::string::~string()
{
if (_str)
{
delete[] _str;
_str = nullptr;
_capacity = _size = 0;
}
}
size_t zhy::string::size()
{
return _size;
}
size_t zhy::string::capacity()
{
return _capacity;
}
bool zhy::string::empty()
{
if (_size == 0)
return true;
else
return false;
}
void zhy::string::reserve(size_t capacity)
{
if (capacity > _capacity) //新开辟的空间比原空间大则增容
{
char* str = new char[capacity + 1]; //实际空间要比_capcity大一个,用来放'\0'
strcpy(str, _str);
delete[] _str;
_str = str;
_capacity = capacity;
}
}
void zhy::string::resize(size_t n, char c) //用字符c填充从n开始后面的空间
{
if (n > _size)
{
if (n>_capacity)
{
reserve(n);
}
for (size_t i = _size; i < n; i++)
{
_str[i] = c;
}
}
_str[n] = '\0';
_size = n;
}
void zhy::string::clean()
{
_size = 0;
_str[_size] = '\0';
}
char zhy::string:: operator[](size_t i)
{
assert(i < _size);
return _str[i];
}
zhy::string::iterator zhy::string::begin()
{
return _str;
}
zhy::string::iterator zhy::string:: end()
{
return _str + _size;
}
void zhy::string::push_back(char c)
{
if (_size == _capacity)
reserve(2 * _capacity+1); //如果_size=_capcity=0,开空间就会失败,所以不能用2*_capcity
_str[_size] = c;
_size++;
_str[_size] = '\0';
}
void zhy::string::append(const char* str)
{
size_t len = strlen(str);
for (size_t i = 0; i < len; i++)
{
push_back(str[i]);
}
}
void zhy::string:: operator+=(char c)
{
push_back(c);
}
void zhy::string::operator+=(const char* s)
{
append(s);
}
void zhy::string::operator+=(const string& str)
{
append(str._str);
}
size_t zhy::string::find(char c, size_t pos) //从pos位置向后找字符c第一次出现的位置
{
for (size_t i = pos; i < _size; i++)
{
if (_str[i] == c)
return i;
}
return npos;
}
size_t zhy::string::find(const char* s, size_t pos) //找子串,若有返回子串的起始位置
{
const char* pch = strstr(_str + pos, s);
if (pch != nullptr)
{
return pch - _str;
}
else
return npos;
}
void zhy::string::insert(size_t pos, char c) //pos位置插入字符c
{
assert(pos <= _size);
if (_size == _capacity) //空间满了增容
{
reserve(2 * _capacity + 1);
}
size_t end = _size + 1; //从'\0'开始向后移动
while (end > pos) //将pos及其后面所有字符向后移动
{
_str[end] = _str[end - 1];
end--;
}
_str[pos] = c;
_size++;
}
void zhy::string::insert(size_t pos, char* s) //pos位置插入字符串s
{
assert(pos <= _size);
size_t len = strlen(s);
if (_size+len > _capacity)
{
reserve(_size + len);
}
size_t end = _size + len;
while (end >= pos + len)
{
_str[end] = _str[end - len]; //从'\0'向后移动
end--;
}
strncpy(_str + pos, s, len);
_size += len;
}
void zhy::string::erase(size_t pos, size_t len) //从pos位置向后删除len个字符
{
assert(pos < _size);
if (len >= _size - pos || len == npos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
size_t first = pos + len; //将pos+len后面的字符向前移动
while (first <= _size)
{
_str[first - len] = _str[first];
first++;
}
_size -= len;
}
}
void zhy::string::swap(string& temp)
{
_size = strlen(temp._str);
_capacity = _size;
std::swap(_str, temp._str); //交换_str,temp._str的空间,_str原有的空间temp出作用域时会释放掉
}
const char* zhy::string::c_str()
{
return _str;
}