目录
再谈swap
string常见接口模拟实现
构造函数 & 析构函数
拷贝构造 & 赋值运算符重载
begin迭代器 & end迭代器
size & swap & clear & c_str
operator[ ] & reserve
push_back & append
operator+= & find
insert & erase
关系运算符重载
流插入 & 流提取
string.h
string.cpp
再谈swap
在string的库中有两个swap,标准库也有swap的实现,这些swap的区别是什么呢?
那么 string为什么要实现一个类成员函数swap一个非成员函数swap呢?
string常见接口模拟实现
构造函数 & 析构函数
string::string(const char* str)
:_size(strlen(str))
{
_str = new char[_size + 1];
_capacity = _size;
strcpy(_str, str);
}
string::~string()
{
delete[]_str;
_str = nullptr;
_size = _capacity = 0;
}
拷贝构造 & 赋值运算符重载
string::string(const string& s)
{
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
string& string::operator=(const string& s)
{
if (*this != s)
{
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete[]_str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
begin迭代器 & end迭代器
string::iterator string::begin()
{
return _str;
}
string::const_iterator string::begin()const
{
return _str;
}
string::iterator string::end()
{
return _str + _size;
}
string::const_iterator string::end()const
{
return _str + _size;
}
size & swap & clear & c_str
size_t string::size()const
{
return _size;
}
void string::swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
void string::clear()
{
_size = 0;
_str[0] = '\0';
}
const char* string::c_str()const
{
return _str;
}
operator[ ] & reserve
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];
}
push_back & append
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++;
}
void string::append(const char* str)
{
size_t len = strlen(str);
if (len + _size > _capacity)
{
reserve(len + _size);
}
strcpy(_str+_size, str);
_size += len;
}
operator+= & find
//加字符
string& string::operator+=(char ch)
{
push_back(ch);
return *this;
}
//加字符串
string& string::operator+=(const char* str)
{
append(str);
return *this;
}
//找字符
size_t string::find(char ch, size_t pos)
{
for (size_t i = pos; i < _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;
}
insert & erase
//插入字符
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 (len + _size > _capacity)
{
reserve(len + _size);
}
size_t end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
--end;
}
memcpy(_str + pos, str, len);
_size += len;
}
void string::erase(size_t pos, size_t len)
{
assert(pos < _size);
if (len >= _size - len)
{
_str[pos] = '\0';
_size = 0;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
关系运算符重载
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 strcmp(_str, s._str) == 0;
}
bool string::operator!=(const string& s) const
{
return !(*this == s);
}
流插入 & 流提取
istream& operator>> (istream& is, string& str)
{
str.clear();
char ch = is.get();
//遇到' '或'\n',流插入停止
while (ch != ' ' && ch != '\n')
{
str += ch;
ch = is.get();
}
return is;
}
ostream& operator<< (ostream& os, const string& str)
{
for (int i = 0; i < str.size(); i++)
{
os << str[i];
}
return os;
}
string.h
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace bit
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
//begin()
iterator begin();
const_iterator begin()const;
//end()
iterator end();
const_iterator end()const;
// 构造
string(const char* str = "");
// 析构
~string();
// 拷贝构造
string(const string& s);
// 赋值运算符重载
string& operator=(const string& s);
//size
size_t size()const;
//operator[]
char& operator[](size_t pos);
const char& operator[](size_t pos)const;
//reserve
void reserve(size_t n);
//push_back
void push_back(char ch);
//append
void append(const char* str);
//+=
string& operator+=(char ch);
string& operator+=(const char* str);
//insert
void insert(size_t pos, char ch);
void insert(size_t pos, const char* str);
//erase
void erase(size_t pos = 0, size_t len = npos);
//find
size_t find(char ch, size_t pos = 0);
size_t find(const char* str, size_t pos = 0);
//swap
void swap(string& s);
//substr
string substr(size_t pos = 0, size_t len = npos);
//relational operators
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;
//clear
void clear();
// c_str
const char* c_str() const;
private:
char* _str;
size_t _size;
size_t _capacity;
const static size_t npos;
};
istream& operator>> (istream& is, string& str);
ostream& operator<< (ostream& os, const string& str);
}
string.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
namespace bit
{
const size_t string::npos = -1;
string::string(const char* str)
:_size(strlen(str))
{
_str = new char[_size + 1];
_capacity = _size;
strcpy(_str, str);
}
string& string::operator=(const string& s)
{
if (*this != s)
{
char* tmp = new char[_capacity + 1];
strcpy(tmp, s._str);
delete[]_str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
string::string(const string& s)
{
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
string::~string()
{
delete[]_str;
_str = nullptr;
_size = _capacity = 0;
}
string::iterator string::begin()
{
return _str;
}
string::const_iterator string::begin()const
{
return _str;
}
string::iterator string::end()
{
return _str + _size;
}
string::const_iterator string::end()const
{
return _str + _size;
}
size_t string::size()const
{
return _size;
}
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];
}
void string::reserve(size_t n)
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete []_str;
_str = tmp;
_capacity = n;
}
}
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++;
}
void string::append(const char* str)
{
size_t len = strlen(str);
if (len + _size > _capacity)
{
reserve(len + _size);
}
strcpy(_str+_size, str);
_size += len;
}
string& string::operator+=(char ch)
{
push_back(ch);
return *this;
}
string& string::operator+=(const char* str)
{
append(str);
return *this;
}
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 (len + _size > _capacity)
{
reserve(len + _size);
}
size_t end = _size + len;
while (end > pos + len - 1)
{
_str[end] = _str[end - len];
--end;
}
memcpy(_str + pos, str, len);
_size += len;
}
void string::erase(size_t pos, size_t len)
{
assert(pos < _size);
if (len >= _size - len)
{
_str[pos] = '\0';
_size = 0;
}
else
{
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
size_t string::find(char ch, size_t pos)
{
for (size_t i = pos; i < _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;
}
const char* string::c_str()const
{
return _str;
}
void string::swap(string& s)
{
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
string string::substr(size_t pos, size_t len)
{
if (len > _size - pos)
{
string s(_str + pos);
return s;
}
else
{
string sub;
sub.reserve(len);
for (size_t i = 0; i < len; i++)
{
sub += _str[pos + i];
}
return sub;
}
}
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 strcmp(_str, s._str) == 0;
}
bool string::operator!=(const string& s) const
{
return !(*this == s);
}
void string::clear()
{
_size = 0;
_str[0] = '\0';
}
istream& operator>> (istream& is, string& str)
{
str.clear();
char ch = is.get();
//遇到' '或'\n',流插入停止
while (ch != ' ' && ch != '\n')
{
str += ch;
ch = is.get();
}
return is;
}
ostream& operator<< (ostream& os, const string& str)
{
for (int i = 0; i < str.size(); i++)
{
os << str[i];
}
return os;
}
}