目录
.h文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <assert.h>
#include <string.h>
using std::ostream;
using std::istream;
namespace stringss
{
class string{
friend ostream& operator<<(ostream& _cout, const stringss::string& s);
friend istream& operator>>(istream& _cin, stringss::string& s);
public:
typedef char* iterator;
string(const char* str = "");
string(const string& s);
string& operator=(const string& s);
~string();
// iterator
iterator begin();
iterator end();
// modify
void push_back(char c);
string& operator+=(char c);
void append(const char* str);
string& operator+=(const char* str);
void clear();
void swap(string& s);
const char* c_str()const;
// capacity
size_t size()const;
size_t capacity()const;
bool empty()const;
void resize(size_t n, char c = '\0');
void reserve(size_t n);
// access
char& operator[](size_t index);
const char& operator[](size_t index)const;
// 返回c在string中第一次出现的位置
size_t find(char c, size_t pos = 0) const;
// 返回子串s在string中第一次出现的位置
size_t find(const char* s, size_t pos = 0) const;
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
string& insert(size_t pos, char c);
string& insert(size_t pos, const char* str);
// 删除pos位置上的元素,并返回该元素的下一个位置
string& erase(size_t pos, size_t len = -1);
private:
char* _str;
size_t _size;
size_t _capacity;
};
//relational operators
bool operator<(const string & s1, const string & s2);
bool operator<=(const string& s1, const string& s2);
bool operator>(const string& s1, const string& s2);
bool operator>=(const string& s1, const string& s2);
bool operator==(const string& s1, const string& s2);
bool operator!=(const string& s1, const string& s2);
};
.cpp文件
#include "stringss.h"
namespace stringss {
//构造函数
string::string(const char* str)
:_size(strlen(str))
, _capacity(_size)
{
_str = new char[_capacity + 1];
strcpy(_str, str);
}
//拷贝构造函数
string::string(const string& s)
:_str(nullptr)
, _size(0)
, _capacity(_size)
{
string temp(s._str);
swap(temp);
}
//重载=函数 与拷贝构造不同的是,这个函数不是构造函数,例如s1 = s2,s1是已经存在的。
string& string::operator=(const string& s) {
string temp(s);
swap(temp);
return *this;
}
//析构函数
string::~string() {
delete[] _str;
_size = 0;
_capacity = 0;
}
//迭代器
string::iterator string::begin() {
return _str;
}
string::iterator string::end() {
return _str + _size;
}
//modify
void string::push_back(char c) {
insert(_size - 1, c);
}
string& string::operator+=(char c) {
push_back(c);
return *this;
}
void string::append(const char* str) {
insert(_size - 1, str);
}
string& string::operator+=(const char* str) {
append(str);
return *this;
}
void string::swap(string& s) {
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
const char* string::c_str()const {
return _str;
}
//capacity
size_t string::size()const {
return _size;
}
size_t string::capacity()const {
return _capacity;
}
bool string::empty()const {
return _size == 0;
}
void string::resize(size_t n, char c) {
if (n < _size) {
memset(_str, c, n);
}
else {
if (n > _capacity) {
reserve(n);
}
memset(_str, c, n);
_size = n;
_str[_size] = '\0';
}
}
void string::reserve(size_t n) {
if (n > _capacity) {
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;
}
}
//access
char& string::operator[](size_t index) {
assert(index < _size);
return _str[index];
}
const char& string::operator[](size_t index)const {
assert(index < _size);
return _str[index];
}
//relational operators
bool operator<(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str()) < 0;
}
bool operator<=(const string& s1, const string& s2)
{
return s1 < s2 || s1 == s2;
}
bool operator>(const string& s1, const string& s2)
{
return !(s1 <= s2);
}
bool operator>=(const string& s1, const string& s2)
{
return s1 > s2 || s1 == s2;
}
bool operator==(const string& s1, const string& s2)
{
return strcmp(s1.c_str(), s2.c_str()) == 0;
}
bool operator!=(const string& s1, const string& s2)
{
return !(s1 == s2);
}
// 返回c在string中第一次出现的位置
size_t string::find(char c, size_t pos)const {
assert(pos < _size);
while (pos < _size) {
if (_str[pos] == c) {
return pos;
}
pos++;
}
return -1;
}
// 返回子串s在string中第一次出现的位置
size_t string::find(const char* s, size_t pos)const {
assert(pos < _size);
size_t index = pos;
size_t start = pos;
while (index < _size) {
if (_str[index] == s[0]) {
start = index;
size_t i = 0;
while (start < _size && i < strlen(s)) {
if (_str[index] == s[i]) {
start++;
i++;
}
else {
break;
}
}
if (i == strlen(s)) {
return index;
}
}
index++;
}
return -1;
}
// 在pos位置上插入字符c/字符串str,并返回该字符的位置
string& string::insert(size_t pos, char c){
assert(pos <= _size);
if (_size == _capacity){
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
size_t end = _size + 1;//防止下标越界
while (end > pos + 1){
_str[end] = _str[end - 1];
end--;
}
_str[end] = c;
_size++;
return *this;
}
string& string::insert(size_t pos, const char* str){
assert(pos <= _size);
int 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--;
}
strncpy(_str + pos + 1, str, len);
_size += len;
return *this;
}
// 删除pos位置上的元素,并返回该元素的下一个位置
string& string::erase(size_t pos, size_t len){
assert(pos < _size);
if (len == -1 || len + pos >= _size){
_str[pos] = '\0';
_size = pos;
}
else{
for (int i = pos + len; i <= _size; i++)
{
_str[i - len] = _str[i];
}
}
_size -= len;
return *this;
}
ostream& operator<<(ostream& _cout, const stringss::string& s){
for (size_t i = 0; i < s._size; i++){
_cout << s[i];
}
return _cout;
}
istream& operator>>(istream& _cin, stringss::string& s){
s.clear();
char ch = _cin.get();
while (ch != ' ' && ch != '\n'){
s += ch;
ch = _cin.get();
}
return _cin;
}
};