目录
前言🥎
1.完整代码🥏
1.1 string.h🏋🏻♀️
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<assert.h>
using namespace std;
namespace spring {
class string {
public:
typedef char* iterator;
typedef const char* const_iterator;
const char* c_str()const {
return _str;
}
//构造函数
string(const char* str = " ")
:_size(strlen(str))
{
_str = new char[_size+1];//开一个空间存储\0
_capacity = _size;
//拷贝数据
strcpy(_str, str);
}
//遍历---const成员和非const成员
// size统计
size_t size() const{//const成员函数:const成员和普通对象都可以调用
return _size;
}
//1.[] + 下标
char& operator[](size_t pos) {
assert(pos < _size);
return _str[pos];
}
const char& operator[]( size_t pos)const {
assert(pos < _size);
return _str[pos];
}
//2. 迭代器 + 范围for
iterator begin() {
return _str;
}
iterator end() {
return _str+_size;
}
const_iterator begin() const{
return _str;
}
const iterator end() const{
return _str + _size;
}
//resreve
void reserve(size_t n) {
if (n > _capacity) {
char* tmp = new char[n+1];
strcpy(tmp, _str);
delete[] _str;
_str = tmp;
_capacity = n;//size没变不需要修改
}
}
//push_back
void push_back(char ch) {
if (_size == _capacity) {//判断是否需要扩容(2倍)
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
_str[_size] = ch;
_size++;
_str[_size] = '\0';
}
// append
void append(const char* str) {
size_t len = strlen(str);
if (_size == 1) {//字符串为空
reserve(_size + 1 + len);
}
if ((len > _capacity - _size)) {
reserve(_size+len);
}
strcpy(_str+_size, str);
_size += len;
}
//+=
string& operator+=(char ch) {
push_back(ch);
return *this;
}
string& operator+=(const char* str) {
append(str);
return *this;
}
//insert---单个字符和string
void insert(size_t pos, char ch) {
assert(pos <= _size);
if (_size == _capacity) {
reserve(_capacity == 0 ? 4 : 2 * _capacity);
}
size_t i = 0;
for (i = _size; i > pos - 1; i--) {
_str[i + 1] = _str[i];
}
_str[pos] = ch;
_size++;
}
void insert(size_t pos, const char* str) {
assert(pos <= _size);
size_t len = strlen(str);
if (len > _capacity-_size) {
reserve(len+_size);
}
size_t i = 0;
for (i = _size; i > pos - 1; i--) {
_str[i + len] = _str[i];
}
strncpy(_str + pos, str, len);
_size += len;
}
//erase---string
void erase(size_t pos, size_t len=npos) {
assert(pos < _size);//不需要==size,因为\0不需要删除
if (len == npos || len >= _size - pos) {
_str[pos] = '\0';
_size = pos;
}
else {
strcpy(_str + pos, _str + pos + len);
_size -= len;
}
}
insert写完之后push_back和append可以复用insert
//void push_back(char ch) {
// insert(_size, ch);
//}
//void append(const char* str) {
// insert(_size, str);
//}
//resize
void resize(size_t n, char ch = '\0') {
if (n > _size) {
reserve(n+1);
for (size_t i = _size; i < n; i++)
{
_str[i] = ch;
}
_str[n] = '\0';
_size = n;
}
else {
_str[n] = '\0';
_size = n;
}
}
//交换s2 s1
void swap(string& s) {
//调库里的
std::swap(_str, s._str);
std::swap(_size, s._size);
std::swap(_capacity, s._capacity);
}
//拷贝 s2(s1)
//方法1
string(const string& s) {
_str = new char[s._capacity + 1];
strcpy(_str, s._str);
_size = s._size;
_capacity = s._capacity;
}
方法2
//string(const string& s) {
// string tmp(s._str);
// swap(tmp);
//}
//赋值
//方法1 s2=s1
string& operator=(const string& s) {
char* tmp = new char[s._capacity + 1];
strcpy(tmp, s._str);
delete[] _str;
_str = tmp;
_size = s._size;
_capacity = s._capacity;
return *this;
}
方法2
//string& operator=(const string& s) {
// string ss(s);//调拷贝构造
// swap(ss);
// return *this;
//}
//find---单个字符和string
size_t find(char ch) const {
for (size_t i = 0; i < _size; i++)
{
if (_str[i] == ch) {
return i;
}
}
return npos;
}
size_t find(const char* stb,size_t pos=0) const {
assert(pos < _size);
const char* p = strstr(_str + pos, stb);
if (p) {
return p - _str;//返回的是下标:指针-指针就是距离(下标)
}
return npos;
}
//substr
string substr(size_t pos = 0, size_t len = npos) {
string tmp;
if (len >= _size - pos) {
for (size_t i = pos; i < _size; i++)
{
tmp += _str[i];
}
}
else {
for (size_t i = pos; i < pos+len; i++)
{
tmp += _str[i];
}
}
return tmp;
}
void clear() {
_size = 0;
_str[_size] = '\0';
}
//析构函数
~string() {
delete[] _str;
_str = nullptr;
_capacity = _size = 0;
}
private:
//构造函数:库里面不处理内置类型,以防万一给缺省值(针对于现代方法的拷贝)
char* _str = nullptr;
size_t _size = 0;
size_t _capacity = 0;
public:
static const int npos;//公有的静态成员变量
};
const int string::npos = -1;
//比较大小
bool operator==(const string& s1, const string s2) {
int ret = strcmp(s1.c_str(), s2.c_str());
return ret==0;
}
bool operator<(const string & s1, const string s2) {
int ret = strcmp(s1.c_str(), s2.c_str());
return ret < 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 !(s1 == s2);
}
//流插入
ostream& operator<<(ostream& out, const string& s) {
for (auto e : s) {
out << e;
}
return out;
}
//流提取
istream& operator>>(istream& in, string& s) {
s.clear();//s本身就有字符,所以+=就是尾插--->所以需要清除掉原本内容
char ch;
ch = in.get();//类似于c的getchar()
while (ch != ' ' && ch != '\n') {
s += ch;
ch = in.get();
}
return in;
}
//遍历检测
void test1() {
string s1("hello world");
string s2;
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
//[] + 下标
for (size_t i = 0; i < s1.size(); i++) {
cout << s1[i] << " ";
}
cout << endl;
//迭代器
string::iterator it1 = s1.begin();
while (it1 != s1.end()) {
cout << *it1 << " ";
it1++;
}
cout << endl;
//范围for
for (auto e : s1) {
cout << e << " ";
}
cout << endl;
}
//push_back + append
void test2() {
string s1("good morning");
string s2;
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s1.push_back('1');
s1.push_back('2');
cout << s1.c_str() << endl;
s2.append("hhhhhhhhhhhhhh");
cout << s2.c_str() << endl;
}
//+= insert erase
void test3() {
string s1("good morning");
string s2;
string s3("hello world");
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s1 += '3';
s2 += "00000000";
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
s3.insert(3, '6');
s1.insert(7, "11111");
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
s1.erase(9, 2);
s3.erase(5, 10);
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
}
//resize 拷贝 赋值
void test4() {
string s1("good morning");
string s2;
string s3("hello world");
cout << s1.c_str() << endl;
cout << s2.c_str() << endl;
cout << s3.c_str() << endl;
s1.resize(15, 'a');
s3.resize(5);
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
s2 = s1;
cout << s2.c_str() << endl;
string s4(s3);
cout << s4.c_str() << endl;
}
//find + substr
void test5() {
string s1("good morning");
string s3("hello world");
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
cout << s1.find('d') << endl;
cout << s1.find("or", 6) << endl;
string s2;
s2 = s3.substr(6, 10);
cout << s2.c_str() << endl;
}
//比较大小 流提取 流插入
void test6() {
string s1("good morning");
string s3("hello world");
cout << s1.c_str() << endl;
cout << s3.c_str() << endl;
cout << (s1 < s3) << endl;
cin >> s1;
cout << s1 << endl;
}
}
1.2 测试图🏀
后语🥊
本次的分享到这里就结束了!!!
PS:小江目前只是个新手小白。欢迎大家在评论区讨论哦!有问题也可以讨论的!期待大家的互动!!!
拜托了帮帮我点赞👍+收藏⭐️+关注➕(这对我真的很重要!!!)