没什么要说的,记录一下代码~
头文件
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace bit
{
class string
{
public:
typedef char* iterator;
typedef const char* const_iterator;
//string();
string(const char* str = "");
~string();
char* c_str()const;
size_t size()const;
size_t capcity()const;
bool empty()const;
string(const string& s);
//string operator=(const string& s);
string operator=(string s);
void swap(string& s);
char& operator[](int n);
const char& operator[](int n) const;
iterator begin();
const_iterator begin()const;
iterator end();
const_iterator end()const;
void reserve(size_t n);
void push_back(char a);
//void append(const bit::string s);
void append(const char* s);
string& operator+=(char a);
//string& operator+=(const bit::string s);
string& operator+=(const char* s);
string& insert(size_t pos, char a);
string& insert(size_t pos, const char* s);
string& erase(size_t pos, size_t len = npos);
bool operator<(const string& s);
bool operator<=(const string& s);
bool operator>(const string& s);
bool operator>=(const string& s);
bool operator==(const string& s);
bool operator!=(const string& s);
size_t find(char c, size_t pos = 0) const;
size_t find(const char* s, size_t pos = 0) const;
string substr(int pos, size_t len = npos);
void clear();
private:
char* _str = nullptr;
int _size = 0;
int _capcity = 0;
const static size_t npos;
//const static size_t npos = -1;//特例:只有const static int可以在这里给缺省值,其他类行如double...都不行.
};
ostream& operator<<(ostream& os, const string& s);
istream& operator>>(istream& is, string& s);
}
实现功能cpp文件
#define _CRT_SECURE_NO_WARNINGS 1
//#include"string.h"
//namespace bit
//{
// const size_t string::npos = -1;//静态成员变量,类里面声明类外面定义
// //但是要是定义在.h中被多个文件包含会重定义,所以声明定义分离时,要在.h中声明.cpp中定义
//
// //string::string()
// // :_size(0)
// // ,_capcity(0)
// //{
// // _str = new char[1] {""};
// //}
// string::string(const char* str)
// :_size(strlen(str))
// {
// _capcity = _size;
// _str = new char[_size + 1];
// strcpy(_str, str);
// }
// string::~string()
// {
// delete[]_str;
// _capcity = _size = 0;
// }
// char* string::c_str()const
// {
// return _str;
// }
// size_t string::size()const
// {
// return _size;
// }
// size_t string::capcity()const
// {
// return _capcity;
// }
// bool string::empty()const
// {
// return _size == 0;
// }
// char& string::operator[](int n)
// {
// return _str[n];
// }
// string::iterator string::begin()
// {
// return _str;
// }
// string::iterator string::end()
// {
// return _str + _size;
// }
//
// const char& string::operator[](int n) const
// {
// return _str[n];
// }
// string::const_iterator string::begin()const
// {
// return _str;
// }
// string::const_iterator string::end()const
// {
// return _str + _size;
// }
//
//
// void string::reserve(size_t n)
// {
// if (n > _capcity)
// {
// char* p = new char[n + 1]; //要多开一个空间,为\0留出空间
// strcpy(p, _str);
// delete[]_str;
// _str = p;
// _capcity = n;
// }
// }
// void string::push_back(char a)
// {
// if (_size == _capcity)
// {
// int newcapcity = 0 == _size ? 4 : 2 * _size;
// reserve(newcapcity);
// }
// _str[_size] = a;
// _size++;
// _str[_size] = '\0';
// }
// //void string::append(const bit::string s)
// //{
// // int sz = s.size();
// // if (_size + sz > _capcity)
// // {
// // int newcapcity = _size + sz;
// // reserve(newcapcity);
// // }
// // strncpy(_str + _size, s._str, sz);
// // _size = _size + sz;
// // _str[_size + 1] = '\0';
// //}
// void string::append(const char* s)
// {
// int sz = strlen(s);
// if (_size + sz > _capcity)
// {
// int newcapcity = _size + sz;
// reserve(newcapcity);
// }
// strcpy(_str + _size, s);
// _size = _size + sz;
// }
// string& string::operator+=(char a)
// {
// this->push_back(a);
// return *this;
// }
// //string& string::operator+=(const bit::string s)
// //{
// // this->append(s);
// // return *this;
// //}
// string& string::operator+=(const char* s)
// {
// this->append(s);
// return *this;
// }
// string& string::insert(size_t pos, char a)
// {
// assert(pos <= _size + 1);
// if (_size == _capcity)
// {
// int newcapcity = 0 == _size ? 4 : 2 * _size;
// reserve(newcapcity);
// }
// int sz = _size + 1;
// while (sz >= (int)pos)
// {
// _str[sz + 1] = _str[sz];
// sz--;
// }
// _str[pos] = a;
// _size++;
// return *this;
// }
// string& string::insert(size_t pos, const char* s)
// {
// assert(pos <= _size + 1);
// int len = strlen(s);
// if (_size + len > _capcity)
// {
// int newcapcity = _size + len;
// reserve(newcapcity);
// }
// int sz = _size + 1;
// while (sz >= (int)pos)
// {
// _str[sz + len] = _str[sz];
// sz--;
// }
// strncpy(_str + pos, s, len); //strcpy会拷贝s的\0,可以用strncpy
// _size = _size + len;
// return *this;
// }
// string& string::erase(size_t pos, size_t len)
// {
// assert(pos <= _size + 1);
// if (len >= _size - pos)
// {
// _str[pos] = '\0';
// _size = pos;
// }
// else
// {
// while ((int)(pos + len) <= (_size + 1))
// {
// _str[pos] = _str[pos + len];
// pos++;
// }
// _size -= len;
// }
// return *this;
// }
//
//
// bool string::operator<(const string& s)
// {
// return strcmp(_str, s._str) < 0;
// }
// bool string::operator<=(const string& s)
// {
// return (*this < s || *this == s);
// }
// bool string::operator>(const string& s)
// {
// return !(*this == s);
// }
// bool string::operator>=(const string& s)
// {
// return !(*this<s);
// }
// bool string::operator==(const string& s)
// {
// return 0 == strcmp(_str, s._str);
// }
// bool string::operator!=(const string& s)
// {
// return !(*this == s);
// }
//
// size_t string::find(char c, size_t pos) const
// {
// assert(pos < _size);
// for (int i = pos; i < _size; i++)
// {
// if (_str[i] == c)
// {
// return i;
// }
// }
// return -1;
// }
// size_t string::find(const char* s, size_t pos) const
// {
// assert(pos < _size);
// char* p = strstr(_str + pos, s);
// return (p - _str);
// }
// string string::substr(int pos, size_t len)
// {
// if (len > (_size - pos))
// {
// string s(_str+pos);
// return s;
// }
// else
// {
// string s1;
// for (int i = 0; i < len; i++)
// {
// s1 += _str[pos + i];
// }
// return s1;
// }
// }
// void string::clear()
// {
// _str[0] = '\0';
// _size = 0;
// }
// ostream& operator<<(ostream& os, const string& s) //临时变量有常性这样才可以打印substr的返回值要传引用
// {
// for (int i = 0; i < s.size(); i++)
// {
// os << s[i];
// }
// return os;
// }
// //istream& operator>>(istream& is, string& s)
// //{
// // s.clear();
// // char ch = is.get();
// // while (ch != ' ' && ch != '\n')
// // {
// // s += ch;
// // ch = is.get();
// // }
// // return is;
// //}
// istream& operator>>(istream& is, string& s)
// {
// s.clear();
// char arr[128];
// char ch = is.get();
// int i = 0;
// while (ch != ' ' && ch != '\n')
// {
// arr[i++] = ch;
// if (i == 127)
// {
// arr[i] = '\0';
// s += arr;
// i = 0;
// }
// ch = is.get();
// }
// if (i != 0)
// {
// arr[i] = '\0';
// s += arr;
// }
// return is;
// }
//
//
// void string::swap(string& s)
// {
// std::swap(_str, s._str);
// std::swap(_size, s._size);
// std::swap(_capcity, s._capcity);
// }
// //老实的写法
// //string::string(const string& s)
// //{
// // char* p = new char[s.size() + 1];
// // strcpy(p, s._str);
// // std::swap(_str, p);
// // _size = s._size;
// // _capcity = s._capcity;
// //}
// //string string::operator=(const string& s)
// //{
// // char* p = new char[s.size() + 1];
// // strcpy(p, s._str);
// // std::swap(_str, p);
// // delete[] p;
// // _size = s._size;
// // _capcity = s._capcity;
// // return *this;
// //}
// //
// //现代写法
// string::string(const string& s)
// {
// string tmp(s._str);
// swap(tmp);
// }
// //string string::operator=(const string& s)
// //{
// // if (this != &s)
// // {
// // string tmp(s._str);
// // swap(tmp);
// // }
// // return *this;
// //}
// string string::operator=(string s)
// {
// if (this != &s)
// {
// swap(s);
// }
// return *this;
// }
//}
测试文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include"string.h"
int main()
{
//bit::string s1("hello world");
//cout << s1.empty() << endl;
//cout << s1.c_str() << endl;
//cout << s1.size() << " " << s1.capcity() << endl;
//for (int i = 0; i < s1.size(); i++)
//{
// //s1[i]++;
// cout << s1[i] << " ";
//}
//cout << endl;
//for (auto& e : s1)
//{
// e++;
// cout << e << " ";
//}
//cout << endl;
//auto i = s1.begin();
//while (i != s1.end())
//{
// cout << *i << " ";
// i++;
//}
//cout << endl;
//const bit::string s2("xxxxxxxxx");
//for (int i = 0; i < s2.size(); i++)
//{
// //s2[i]++; //err
// cout << s2[i] << " ";
//}
//cout << endl;
//for (auto& e : s2)
//{
// cout << e << " ";
//}
//cout << endl;
//auto i = s2.begin();
//while (i != s2.end())
//{
// cout << *i << " ";
// i++;
//}
//bit::string s3("hello");
bit::string s4("world");
//s3.push_back('x');
//cout << s3.c_str() << endl;
//s3.append(" bit");
//cout << s3.c_str() << endl;
err ????
s3.append(s4);
cout << s3.c_str() << endl;
//s3 += 'y';
//cout << s3.c_str() << endl;
//s3 += "world";
//cout << s3.c_str() << endl;
//s3.erase(5);
//cout << s3.c_str() << endl;
//s3.insert(5, 'x');
//s3.insert(0, 'x');
//cout << s3.c_str() << endl;
//s3.insert(0,"yyyyy");
//s3.insert(6,"zzz");
s3.insert(3,"zzz");
//cout << s3.c_str() << endl;
//s3.erase(6,3);
//cout << s3.c_str() << endl;
//bit::string s11("hello");
//bit::string s12("hello");
//bit::string s13("helloa");
//bit::string s14("hallo");
//cout << (s11 == s12) << endl;
//cout << (s11 != s13) << endl;
//cout << (s11 == s13) << endl;
//cout << (s11 != s12) << endl;
//cout << (s11 > s13) << endl;
//cout << (s11 <= s13) << endl;
//cout << (s11 < s14) << endl;
//cout << (s11 > s12) << endl;
//cout << (s11 >= s12) << endl;
//bit::string s4("https://leetcode.cn/problems/first-unique-character-in-a-string/description/");
//cout << s4 << endl;
//size_t cut1 = s4.find(':');
//cout << s4.substr(0, cut1) << endl;
//size_t cut2 = s4.find('/', cut1 + 3);
//cout << s4.substr(cut1 + 3, cut2 - (cut1 + 3)) << endl;
//cout << s4.substr(cut2 + 1) << endl;
//cin >> s4;
//cout << s4 << endl;
//bit::string s5("hello");
//bit::string s6(s5);
//cout << s5 << endl;
//cout << s6 << endl;
//s6 = "world";
//cout << s6 << endl;
return 0;
}