版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/woyaoxuechengxu/article/details/70483645
自己实现的smallstring类
一个简单的string类,具有string基本的功能,用来练练手复习c++的基础知识.思路较为简单,没有很复杂的地方
header文件如下:
#ifndef SAMLLSTRING_H_
#define SAMLLSTRING_H_
#include <istream>
namespace SmallString
{
class String
{
public:
String();
String(const char *);
String(const String&);
String(String &&);//move 构造函数
~String();
String& operator=(const char*);
String& operator=(const String &);
String& operator=(String&& str);
bool operator==(const char*);
bool operator==(const String&);
char &operator[](int);
String& operator+=(const char *);
String& operator+=(const char);
String& operator+=(const String&);
String operator+(const char*);
String operator+(const char);
String operator+(const String&);
int size(){ return _size; }
char *c_str(){ return _string; }
friend std::ostream &operator<<(std::ostream &, String &str);
private:
void destroy();
int _size;
char *_string;
};
}
#endif
类定义文件:
#include "simpleSTL.h"
#include <iostream>
#include <cassert>
//关闭warning 4996
#pragma warning(disable: 4996)
namespace SmallString
{
String::String()
{
_size = 0;
_string = nullptr;
}
void String::destroy()
{
if (_string)
{
delete _string;
_size = 0;
}
}
String::~String()
{
destroy();
}
String::String(const char* str)
{
if (!str)
{
_size = 0;
_string = nullptr;
}
else
{
_size = strlen(str);
_string = new char[_size + 1];
strcpy(_string, str);
_string[_size] = 0;
}
}
String::String(const String& str)
{
if (str._size)
{
_size = str._size;
_string = new char[_size + 1];
strcpy(_string, str._string);
_string[_size] = 0;
}
else
{
_size = 0;
_string = nullptr;
}
}
String::String(String&& str)
{
_size = str._size;
_string = str._string;
str._string = nullptr;
}
String& String::operator=(const char* str)
{
return *this = String(str);
}
String& String::operator=(const String& str)
{
destroy();
if (!str._string)
{
_size = 0;
_string = nullptr;
return *this;
}
if (*this == str)
return *this;
_size = str._size;
_string = new char[_size + 1];
strcpy(_string, str._string);
_string[_size] = 0;
return *this;
}
String& String::operator=(String&& str)
{
if (str._string != _string)
{
_size = str._size;
_string = str._string;
str._size = 0;
str._string = nullptr;
}
return *this;
}
bool String::operator==(const char* str)
{
return (*this == String(str));
}
bool String::operator==(const String& str)
{
if (str._size != _size)
{
return false;
}
for (int i = 0; i < str._size; ++i)
{
if (str._string[i] != _string[i])
return false;
}
return true;
}
char& String::operator[](int i)
{
if (_string)
{
return this->_string[i];
}
else
{
throw "nullpointerexception";
}
}
String& String::operator+=(const char c)
{
int size = _size + 1;
char* newString = new char[size+1];
strcpy(newString, _string);
destroy();
newString[_size] = c;
newString[size] = 0;
_string = std::move(newString);
_size = strlen(_string);
return *this;
}
String& String::operator+=(const char* str)
{
return (*this += String(str));
}
String& String::operator+=(const String& str)
{
if (str._string == nullptr)
{
return *this;
}
int size = str._size + _size;
char *newString = new char[size + 1];
strcpy(newString, _string);
strcpy(newString + _size, str._string);
destroy();
newString[size] = 0;
_string = std::move(newString);
_size = strlen(_string);
return *this;
}
String String::operator+(const char c)
{
String newString(*this);
newString += c;
return std::move(newString);
}
String String::operator+(const char* str)
{
return (*this + String(str));
}
String String::operator+(const String& str)
{
assert(str._string);
String newString(*this);
newString += str;
return std::move(newString);
}
std::ostream &operator<<(std::ostream &os, String &str)
{
return os << str._string;
}
}
测试main函数如下:
#include "simpleSTL.h"
#include <iostream>
#include <cassert>
using namespace std;
using namespace SmallString;
int main()
{
String s1("a");
String s2("bcd");
String s3(s2);
cout << s1 << " " << s2 << " " << s3 << " ";
cout << endl;
s3 = s1;
s1 = "adc";
cout << s1 << " " << s2 << " " << s3 << " ";
cout << endl;
s2 = s3;
assert(s2 == s3);
cout << s1[1] << endl;
s1 = "lwj";
s2 = "love";
s3 = "study";
s1 += s2;
s1 += s3;
s3 = s1 + s2 + s3;
s3 = s3 + "!";
cout << s1 << " " << s2 << " " << s3 << " ";
cout << endl;
}
编译运行环境为vs2013,测试中未出现问题.
大家如果有更好的建议,可以在评论区提出来,我很乐意接受.有不足之处也请大家指出,万分感谢