出处:https://github.com/chenshuo/recipes/blob/master/string/StringTrivial.h
#include<utility>
#include<assert.h>
#include<string.h>
class String
{
String() : data_(new char[1])
{
*data_ = '\0';
}
String(const char* str) : data_(new char[strlen(str) + 1])
{
strcpy(data_, str);
}
String(const String& str) : data_(new char[str.size() + 1]
{
strcpy(data_, str.c_str())
}
String(String&& str) noexcept : data_(str.data_)
{
str.data_ = nullptr;
}
String& operater=(String str)
{
swap(str);
return *this;
}
~String() noexcept
{
delete[] data_;
}
size_t size()
{
return strlen(data_);
}
const char* c_str()
{
return data_;
}
void swap(string& rhs)
{
std::swap(data_, rhs.data_);
}
private:
char* data_;
}