目录
string类实现
我们采用声明和定义分离的方式
首先是.h文件
//
// string.hpp
// String
//
// Created by 南毅 on 2024/5/8.
//
#ifndef string_hpp
#define string_hpp
#endif /* string_hpp */
#include <iostream>
using namespace std;
#include <assert.h>
#include <string.h>
namespace nanyi{
class String{
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos;
public:
typedef char* iterator;
typedef const char* const_iterator;
//s构造函数、初始化列表
//拷贝构造
String(const char* str = "");
String();
String(const String& s);
String& operator=(const String &s);
~String();
const char* c_str() const;
size_t size() const;
char& operator[](size_t pos);
const char& operator[](size_t pos) const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
void reserve(size_t n);
void push_back(char ch);
void append(const char* str);
String& operator+=(char ch);
String& operator+=(const char* str);
void insert(size_t pos, char ch);
void insert(size_t pos, const char* str);
void erase(size_t pos = 0, size_t len = npos);
size_t find(char ch, size_t pos = 0);
size_t find(const char* str, size_t pos = 0);
void swap(String& s);
String substr(size_t pos = 0, size_t len = npos);
bool operator<(const String& s) const;
bool operator>(const String& s) const;
bool operator<=(const String& s) const;
bool operator>=(const String& s) const;
bool operator==(const String& s) const;
bool operator!=(const String& s) const;
void clear();
};
istream& operator>> (istream& is, string& str);
ostream& operator<< (ostream& os, const string& str);
}
1.构造函数(三种)
//构造函数
String::String(const char* str)
:_size(strlen(str))
{
_capacity = _size;
_str = new char[_size + 1];
strcpy(_str, str);
}
//默认构造
String::String(){
_str = new char[1]{'\0'};
_size = _capacity = 0;
}
//拷贝构造
String::String(const String& s)
{
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
2.c_str()函数
因为C形式字符串是内置类型,所以使用cout打印时编译器会自动识别类型,相当于使用%s进行打印字符串,只需要返回字符串数组首字符地址即可
const char* String:: c_str() const {
return _str;
}
3.operator[] 重载
可以以数组的形式,通过下标访问数据
char& String::operator[](size_t pos){
assert(pos < _size);
return _str[pos];
}
const char& String::operator[](size_t pos) const{
assert(pos < _size);
return _str[pos];
}
4.size()函数实现
size_t String::size() const
{
return _size;
}
5.迭代器
String::iterator String::begin(){
return _str;
}
String::iterator String::end(){
return _str + _size;
}
String::const_iterator String::begin() const{
return _str;
}
String::const_iterator String::end() const{
return _str + _size;
}
6.reserve()函数实现
void String::reserve(size_t n){
char* tmp = new char[n+1];
strcpy(tmp, _str);
delete [] _str;
_str = tmp;
_capacity = n;
}
7.push_back()函数实现
void String::push_back(char ch){
if (_size == _capacity) {
size_t newcapacity = _capacity == 0? 4 : _capacity*2;
reserve(newcapacity);
}
_str[_size] = ch;
_str[_size + 1] = '\0';
_size++;
}
8.append()函数实现
void String::append(const char* str){
size_t len = strlen(str);
if (_size + len > _capacity) {
reserve(_size + len);
}
strcpy(_str + _size, str);
_size += len;
}
9.operator+=实现
//+=一个字符
String& String::operator+=(char ch){
push_back(ch);
return *this;
}
//+=一个字符串
String& String::operator+=(const char* str){
append(str);
return *this;
}
10.insert() 实现
void String::insert(size_t pos, char ch){
assert(pos <= _size);
if (_size == _capacity) {
size_t newcapacity = _capacity == 0? 4 : _capacity*2;
reserve(newcapacity);
}
size_t end = _size + 1;
while (end > pos) {
_str[end] = _str[end-1];
end--;
}
_str[pos] = ch;
_size++;
}
void String::insert(size_t pos, const char* str){
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity) {
reserve(_size + len);
}
size_t end = _size + len;
while (end >= pos + len) {
_str[end] = _str[end-len];
end--;
}
memcpy(_str + pos, str, len);
_size += len;
}
11.erase()函数实现
void String::erase(size_t pos, size_t len){
assert(pos <= _size);
if (len >= _size - pos) {
_str[pos] = '\0';
_size = pos;
}else{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
12.find()函数实现
size_t String::find(char ch, size_t pos){
for (size_t i = 0; 0 < _size; i++) {
if (_str[i] == ch) {
return i;
}
}
return npos;
}
size_t String::find(const char* str, size_t pos){
char* p = strstr(_str + pos,str);
return p - _str;
}
13.swap()函数实现
void String::swap(String& s){
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
14.substr()函数实现
String String::substr(size_t pos, size_t len){
if (len > _size - pos) {
String sub(_str + pos);
return sub;
}else{
String sub = "";
sub.reserve(len);
for (size_t i = 0; i < len; i++) {
sub += _str[pos+i];
}
return sub;
}
}
15.运算符重载比大小
bool String::operator<(const String& s) const{
return strcmp(_str, s._str) < 0;
}
bool String::operator==(const String& s) const{
return strcmp(_str, s._str) == 0;
}
bool String::operator>(const String& s) const
{
return !(*this <= s);
}
bool String::operator<=(const String& s) const
{
return *this < s || *this == s;
}
bool String::operator>=(const String& s) const
{
return !(*this < s);
}
bool String::operator!=(const String& s) const
{
return !(*this == s);
}
16.clear
void String::clear()
{
_str[0] = '\0';
_size = 0;
}
17.流插入 流提取实现
istream& operator>> (istream& is, string& str){
str.clear();
char ch = is.get();
while (ch != ' ' && ch != '\n') {
str += ch;
ch = is.get();
}
return is;
}
ostream& operator<< (ostream& os, const string& str){
for (size_t i = 0; i < str.size(); i++)
{
os << str[i];
}
return os;
}
18.析构函数
//析构函数
String::~String(){
delete[] _str;
_str = nullptr;
_capacity = _size = 0;
}
补充修改
1.push_back优化
void String::push_back(char ch){
insert(_size, ch);
}
2.append优化
void String::append(const char* str){
insert(_size, str);
}
3.insert函数另外一种方法
void String::insert(size_t pos, char ch){
assert(pos <= _size);
if (_size == _capacity) {
size_t newcapacity = _capacity == 0? 4 : _capacity*2;
reserve(newcapacity);
}
size_t end = _size;
while (end >= (int)pos)
{
_str[end + 1] = _str[end];
--end;
}
_str[pos] = ch;
++_size;
}
void String::insert(size_t pos, const char* str){
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity) {
reserve(_size + len);
}
int end = _size;
while (end >= (int)pos)
{
_str[end + len] = _str[end];
--end;
}
memcpy(_str + pos, str, len);
_size += len;
}