简单实现std::string类及一些常用的成员函数。
迭代器,使用指针简单代替。
目录
1、String.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <assert.h>
using namespace std;
namespace Lzc
{
class string
{
private:
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
static const size_t npos; // 声明,static ,共享,封装
public:
typedef char* iterator ;
typedef const char* const_iterator;
iterator begin()
{
return &_str[0];
}
iterator end()
{
return &_str[_size];
}
const_iterator begin() const
{
return &_str[0];
}
const_iterator end() const
{
return &_str[_size];
}
// 默认构造
string(const char* str = "")// 空字符串应该是'\0'
{
_size = strlen(str);
_capacity = _size; // _capacity 不包括'\0','\0'另外开空间
_str = new char[_size + 1];
strcpy(_str, str);
}
void swap(string& tmp)
{
std::swap(_str, tmp._str);
std::swap(_size, tmp._size);
std::swap(_capacity, tmp._capacity);
}
// 拷贝构造
string(const string& str)
{
string tmp(str._str);
swap(tmp);
}
//赋值重载
string& operator=(const string& str)
{
string tmp(str);// 拷贝构造
swap(tmp);
return *this;
}
// 析构
~string()
{
if(_str)
{
delete[] _str;
_str = nullptr;
_size = _capacity = 0;
}
}
const char* c_str() const
{
return _str;
}
size_t size() const
{
return _size;
}
void resize(size_t n)
{
if(n<_size)
_size = n;
else
{
reserve(n);
for (size_t i = _size; i < n; i++)
{
_str[i] = ' ';
++_size;
}
}
}
size_t capacity() const
{
return _capacity;
}
void clear()
{
_str[0] = '\0';
_size = 0;
}
bool empty() const
{
return _size == 0 ? true : false;
}
char& operator[](size_t pos)
{
assert(pos < _size);
return _str[pos];
}
const char& operator[](size_t pos) const
{
assert(pos < _size);
return _str[pos];
}
void reserve(size_t n); // n>_capacity 扩容,其他情况,不执行
void push_back(char ch);
void append(const char* str);
string& operator+=(char ch);
string& operator+=(const char* str);
void insert(size_t pos, char ch);
void insert(size_t pos, const char* str);
void erase(size_t pos, size_t len = npos);
size_t find(char ch, size_t pos = 0);
size_t find(const char* str, size_t pos = 0);
string substr(size_t pos = 0, size_t len = npos);
};
// 类中函数,第一个参数必须是this指针
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);
ostream& operator<<(ostream& out, const string& s);
istream& operator>>(istream& in, string& s);
}
2、String.cpp
#include "String.h"
namespace Lzc
{
const size_t string::npos = -1;
void string::reserve(size_t n) // n不包括'\0',n>_capacity 扩容,其他情况,不执行
{
if (n > _capacity)
{
char* tmp = new char[n + 1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n; // reserve 不改变 _size
}
}
void string::push_back(char ch)
{
if (_size == _capacity)
reserve(_capacity == 0 ? 4 : 2 * _capacity);
_str[_size++] = ch;
_str[_size] = '\0';
}
void string::append(const char* str)
{
size_t len = strlen(str);
if (_size + len > _capacity)
reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
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)
reserve(_capacity == 0 ? 4 : 2 * _capacity);
for (size_t i = _size + 1; i >= pos + 1; i--)
{
_str[i] = _str[i - 1];
}
_str[pos] = ch;
_size++;
}
void string::insert(size_t pos, const char* str)
{
assert(pos <= _size);
size_t len = strlen(str);
if (_size + len > _capacity)
reserve(_size + len > 2 * _capacity ? _size + len : 2 * _capacity);
for (size_t i = _size + len; i >= pos+len; i--)
{
_str[i] = _str[i - len];
}
for (size_t i = 0; i < len; i++)
{
_str[pos + i] = str[i];
}
_size += len;
}
void string::erase(size_t pos, size_t len)
{
assert(pos < _size);
if (len >= _size - pos)
{
_str[pos] = '\0';
_size = pos;
}
else
{
for (size_t i = pos; i <= _size - len; i++)
{
_str[i] = _str[i + len];
}
_size -= len;
}
}
size_t string::find(char ch, size_t pos)
{
assert(pos < _size);
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)
{
assert(pos < _size);
for (size_t i = pos; i < _size; i++)
{
size_t j = 0;
size_t x = i;
while (_str[x] == str[j] && j < strlen(str))
{
x++;
j++;
}
if (j == strlen(str))
return i;
}
return npos;
}
string string::substr(size_t pos, size_t len)
{
assert(pos < _size);
if (len > _size - pos)
len = _size - pos;
string substr;
substr.reserve(len);
for (size_t i = 0; i < len; i++)
{
substr += _str[pos + i];
}
return substr;
}
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);
}
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);
}
ostream& operator<<(ostream& out, const string& s)
{
for (auto ch : s)
{
out << ch;
}
return out;
}
istream& operator>>(istream& in, string& s)
{
char ch = in.get(); // 获取每一个字符,不会跳
// in >> ch; 会自动跳过,空格、换行符
const size_t N = 256; // 一个缓存,减少s的频繁扩容
char tmp[N];
size_t i = 0;
while (ch != ' ' && ch != '\n')
{
tmp[i] = ch;
i++;
if (i == N - 1)
{
tmp[i] = '\0';
s += tmp;
i = 0;
}
ch = in.get();
}
if (i > 0)
{
tmp[i] = '\0';
s += tmp;
}
return in;
}
}
3、Test.cpp
#include "String.h"
namespace Lzc
{
void test_string1()
{
string s1;
string s2("222");
cout << s2.c_str() << endl;
string s3(s2);
cout << s3.c_str() << endl;
string s4;
s4 = s2;
cout << s4.c_str() << endl;
}
void test_string2()
{
string s1("aaa");
// 下标 + [ ]
for (size_t i = 0; i < s1.size(); i++) // aaa
{
cout << s1[i];
}
cout << endl;
// 迭代器
// string::iterator it = s1.begin();
auto it = s1.begin();
while (it != s1.end()) // ccc
{
*it += 2;
cout << *it;
++it;
}
cout << endl;
// 自动迭代,底层也是迭代器
for (auto s : s1) // eee,注意:是将s1中的数据,赋值给s,值传递
{ // for (auto& s : s1),才会改变s1
s += 2; // for (const auto& s : s1),减少拷贝,s1不能修改
cout << s;
}
cout << endl;
}
void test_string3()
{
string s1,s2;
cout << s1.capacity() << endl;
s1.reserve(6);
cout << s1.capacity() << endl;
s1.reserve(4);
cout << s1.capacity() << endl;
cout << endl;
cout << s2.size() << endl;
cout << s2.capacity() << endl;
s2.resize(6);
cout << s2.size() << endl;
cout << s2.capacity() << endl;
s2.resize(4);
cout << s2.size() << endl;
cout << s2.capacity() << endl;
}
void test_string4()
{
string s1("c");
s1.append("d");
s1.push_back('e');
s1 += 'f';
cout << s1.c_str() << endl;
s1.insert(1, 'g');
cout << s1.c_str() << endl;
s1.insert(0, 'g');
cout << s1.c_str() << endl;
s1.insert(s1.size(), 'g');
cout << s1.c_str() << endl;
s1.insert(1, "hh");
cout << s1.c_str() << endl;
s1.insert(0, "hh");
cout << s1.c_str() << endl;
s1.insert(s1.size(), "hh");
cout << s1.c_str() << endl;
s1.erase(s1.size()-2, 2);
cout << s1.c_str() << endl;
s1.erase(0, 2);
cout << s1.c_str() << endl;
s1.erase(1, 2);
cout << s1.c_str() << endl;
s1.erase(s1.size() - 1, 1);
cout << s1.c_str() << endl;
s1.erase(0, 1);
cout << s1.c_str() << endl;
s1.erase(1, 1);
cout << s1.c_str() << endl;
}
void test_string5()
{
string s1("abcdefg");
cout << s1.find('a', 0) << endl;
cout << s1.find('a', 1) << endl;
cout << s1.find("cd", 0) << endl;
cout << s1.find("cd", 1) << endl;
cout << s1.substr(0, 2).c_str() << endl;
cout << s1.substr(2, 2).c_str() << endl;
}
void test_string6()
{
string s1("aaa");
string s2("bbb");
cout << (s1 < s2) << endl;
cout << (s1 <= s2) << endl;
cout << (s1 == s2) << endl;
cout << (s1 != s2) << endl;
cout << (s1 > s2) << endl;
cout << (s1 >= s2) << endl;
}
void test_string7()
{
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
}
}
int main()
{
Lzc::test_string3();
return 0;
}