当前位置 博文首页 > ENSHADOWER的博客:【STL】之String类的模拟实现
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<assert.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<Windows.h>
#include<iostream>
#include<string>
using namespace std;
//string实现
namespace myself
{
class String
{
public:
typedef char* Iterator; //迭代器(类似指针)
public:
String(const char* str = "") //构造函数
{
if (str == nullptr)
str = "";
_size = strlen(str);
_capacity = _size;
_str = new char[_capacity + 1];
strcpy(_str, str);
}
String(const String& s) //拷贝构造
:_str(new char[s._capacity + 1])
, _size(s._size)
, _capacity(s._capacity)
{
strcpy(_str, s._str);
}
String& operator=(const String& s) //赋值运算符重载
{
if (this != &s)
{
char* pstr = new char[s._capacity + 1];
strcpy(pstr, s._str);
delete[] _str;
_str = pstr;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
~String() //析构函数
{
if (_str)
{
delete[] _str;
_str = nullptr;
_size = 0;
_capacity = 0;
}
}
Iterator Begin()
{
return _str;
}
Iterator End()
{
return _str + _size;
}
void Reserve(size_t newCapacity) //增容(为字符串预留空间)
{
if (_capacity < newCapacity) //如果新容量大于旧容量,则开辟新空间
{
char* newstr = new char[newCapacity + 1];
strcpy(newstr,_str);
delete[] _str;
_str = newstr;
_capacity = newCapacity;
}
}
void Resize(size_t newSize, char c = char())// 将有效字符的个数该成newSize个,多出的空间用字符c填充
{
if (newSize > _capacity)
{
Reserve(newSize);
memset(_str + _size, c, newSize - _size); //将原字符串末尾开始到新字符串大小结束这之间用c填充
}
_size = newSize;
_str[newSize] = '\0';
}
void PushBack(char c) //在字符串后尾插c字符
{
if (_size == _capacity)
Reserve(_capacity * 2 + 2);
_str[_size++] = c;
_str[_size] = '\0';
}
void append(const char* s) //在字符串后面追加一个字符串
{
size_t n = strlen(s);
size_t i = 0;
for (i = 0; i < n; i++)
{
PushBack(*s);
s++;
}
/*
size_t len = strlen(s);
if(len>_capacity-_size)
Reserve(2 * _capacity+len);
strcat(_str,str); //strcat将'\0'直接连接上去了
_size += len;
*/
}
String& operator+=(const char* s) //在字符串后面追加c个数字符串
{
size_t sz = strlen(s);
if (_size + sz > _capacity)
Reserve(_capacity * 2 + sz);
for (size_t i = 0; i < sz; i++)
{
PushBack(*s);
s++;
}
return *this;
}
char& operator[](size_t index) //返回index位置的字符,非const string类对象调用
{
return _str[index];
}
const char& operator[](size_t index)const //返回index位置的字符,const string类对象调用
{
return _str[index];
}
size_t Size()const
{
return _size;
}
size_t Capacity()const
{
return _capacity;
}
bool empty()const //判空
{
return 0 == _size;
}
int Find(char c, size_t pos) //正序查找
{
for (size_t i = pos; i < _size; i++)
{
if (c == _str[i])
return i;
}
return npos;
}
int rFind(char c, size_t pos) //倒序查找
{
for (int i = _size - 1 - pos; i >= 0; --i)
{
if (c == _str[i])
return i;
}
return npos;
}
const char* C_Str()const //以C打印
{
return _str;
}
void Clear() //清空
{
_size = 0;
_str[_size] = '\0';
}
void Swap(String& s) //交换
{
swap(_str, s._str);
swap(_size, s._size);
swap(_capacity, s._capacity);
}
private:
friend ostream& operator<<(ostream& _cout, const myself::String& s);
private:
char* _str;
size_t _size;
size_t _capacity;
const static int npos;
};
}
ostream& myself::operator<<(ostream& _cout, const myself::String& s)
{
cout << s._str;
return _cout;
}
-------------------------------测试函数-----------------------------------------
void Test1()
{
myself::String s1;
myself::String s2("hello world");
myself::String s3(s2);
s1 = s3;
cout << s1 << " " << s2 << " " << s3 << " " << endl;
}
void Test2()
{
myself::String s1("hello");
s1.PushBack('w');
char* str1 = "or";
s1.append(str1);
char* str2 = "ld";
s1 += str2;
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity()<< endl;
}
void Test3()
{
myself::String s1("hello");
auto it = s1.Begin();// 利用迭代器打印string中的元素
while (it != s1.End())
{
cout << *it++ << " ";
}
cout << endl;
myself::String s2("fine");
s1.Swap(s2);
cout << s1 << " " << s2 << endl;
}
void Test4()
{
myself::String s1("hello");
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity() << endl;
s1.Resize(10, 'd');
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity() << endl;
s1.Resize(20);
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity() << endl;
s1.Resize(6);
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity() << endl;
s1.Reserve(30);
cout << s1 << endl;
cout << s1.Size() << endl;
cout << s1.Capacity() << endl;
}
int main()
{
//Test1();
//Test2();
//Test3();
Test4();
system("pause");
return 0;
}
?
cs