头文件
#pragma once
#include <iostream>
class String
{
public:
String();
String(const char *);
String(const String &);
virtual ~String();
char & operator[](int) const;
String & operator+(const String &);
String & operator+(const char *);
String & operator+(const char);
String & operator+=(const String &);
String & operator+=(const char *);
String & operator+=(const char);
bool operator<(const String &);
bool operator>(const String &);
bool operator==(const String &);
friend std::ostream & operator<<(std::ostream &, const String &);
friend std::istream & operator>>(std::istream &, const String &);
int length() const;
int size() const;
int capacity() const;
void push_back(const String &);
void push_back(const char *);
void push_back(const char);
void pop_back();
void push_front(const String &);
void push_front(const char *);
void push_front(const char);
friend void swap(String &, String &);
friend void reverse(String &);
friend int stoi(String &);
friend void erase(String &);
void print_mystring() const;
private:
char * m_str = NULL;
int m_size = 0;
int m_capacity = 0;
static const int m_cap = 15;
};
String to_string(int);
String to_string(unsigned int);
String to_string(float);
String to_string(double);
源文件
#include "MyString.h"
String::String()
{
printf("无参构造函数\n");
m_size = 0;
m_capacity = m_cap;
m_str = new char[1];
m_str[0] = '\0';
}
String::String(const char * str)
{
printf("有参构造函数\n");
m_size = strlen(str);
m_capacity = m_size % m_cap > 0 ? m_size / m_cap + 1 : m_size / m_cap;
m_capacity = m_cap * (m_size == 0 ? 1 : m_capacity);
m_str = new char[m_capacity + 1];
memset(m_str, 0, m_capacity);
memcpy(m_str, str, m_size);
m_str[m_size] = '\0';
}
String::String(const String & str)
{
printf("拷贝构造函数\n");
m_size = str.m_size;
m_capacity = str.m_capacity;
m_str = new char[m_capacity + 1];
memset(m_str, 0, m_capacity);
memcpy(m_str, str.m_str, m_size);
m_str[m_size] = '\0';
}
String::~String()
{
printf("析构函数\n");
}
char & String::operator[](int index) const
{
if (this->m_size <= index)
{
throw;
}
return *(m_str + index);
}
String & String::operator+(const String & str)
{
if (m_size + str.m_size >= m_capacity)
{
m_capacity += str.m_capacity;
char * temp = new char[m_capacity + 1];
memset(temp, 0, m_capacity);
memcpy(temp, m_str, m_size);
memcpy(temp + m_size, str.m_str, str.m_size);
m_size += str.m_size;
temp[m_size] = '\0';
m_str = temp;
}
else
{
memcpy(m_str + m_size, str.m_str, str.m_size);
m_size += str.m_size;
m_str[m_size] = '\0';
}
return *this;
}
String & String::operator+(const char * str)
{
int slen = strlen(str);
if (m_size + slen >= m_capacity)
{
m_capacity = (m_size + slen) % m_cap > 0 ? (m_size + slen) / m_cap + 1 : (m_size + slen) / m_cap;
m_capacity = m_cap * ((m_size + slen) == 0 ? 1 : m_capacity);
char * temp = new char[m_capacity + 1];
memset(temp, 0, m_capacity);
memcpy(temp, m_str, m_size);
memcpy(temp + m_size, str, slen);
m_size += slen;
temp[m_size] = '\0';
m_str = temp;
}
else
{
memcpy(m_str + m_size, str, slen);
m_size += slen;
m_str[m_size] = '\0';
}
return *this;
}
String & String::operator+(const char chr)
{
if (m_size + 1 >= m_capacity)
{
m_capacity = (m_size + 1) % m_cap > 0 ? (m_size + 1) / m_cap + 1 : (m_size + 1) / m_cap;
m_capacity = m_cap * ((m_size + 1) == 0 ? 1 : m_capacity);
char * temp = new char[m_capacity + 1];
memset(temp, 0, m_capacity);
memcpy(temp, m_str, m_size);
memcpy(temp + m_size, &chr, 1);
m_size += 1;
temp[m_size] = '\0';
m_str = temp;
}
else
{
memcpy(m_str + m_size, &chr, 1);
m_size += 1;
m_str[m_size] = '\0';
}
return *this;
}
String & String::operator+=(const String & str)
{
if (m_size + str.m_size >= m_capacity)
{
m_capacity += str.m_capacity;
char * temp = new char[m_capacity + 1];
memset(temp, 0, m_capacity);
memcpy(temp, m_str, m_size);
memcpy(temp + m_size, str.m_str, str.m_size);
m_size += str.m_size;
temp[m_size] = '\0';
m_str = temp;
}
else
{
memcpy(m_str + m_size, str.m_str, str.m_size);
m_size += str.m_size;
m_str[m_size] = '\0';
}
return *this;
}
String & String::operator+=(const char * str)
{
int slen = strlen(str);
if (m_size + slen >= m_capacity)
{
m_capacity = (m_size + slen) % m_cap > 0 ? (m_size